import { AssetBinder , AssetBindings } from '@diez/compiler-core' ; import { File , FileType , Image } from '@diez/prefabs' ; import { IosBinding , IosLanguages , IosOutput } from '@diez/targets' ; import { basename , dirname , join , parse } from 'path' ; import { getPathToFileContents , sourcesPath } from '../../utils' ; const assetCatalog = 'Images.xcassets' ; const examplesPath = join ( sourcesPath , 'ios' , 'examples' , 'Image' ) ; const makeContentsInfoJson = ( bundleId ) => ( { version : 1 , author : bundleId , } ) ; const makeContentsJson = ( bundleId ) => ( { info : makeContentsInfoJson ( bundleId ) , } ) ; const makeFolderContentsJson = ( bundleId ) => ( { info : makeContentsInfoJson ( bundleId ) , properties : { 'provides-namespace' : true , } , } ) ; const makeImagesetContentsImageJson = ( filename , scale ) => ( { filename , scale , idiom : 'universal' , } ) ; const makeImagesetContentsJson = ( bundleId , file , file2x , file3x ) => ( { info : makeContentsInfoJson ( bundleId ) , images : [ makeImagesetContentsImageJson ( file , '1x' ) , makeImagesetContentsImageJson ( file2x , '2x' ) , makeImagesetContentsImageJson ( file3x , '3x' ) , ] , } ) ; const addCatalog = ( assetBindings , bundleId ) => { if ( assetBindings . has ( assetCatalog ) ) { return ; } const json = makeContentsJson ( bundleId ) ; const contents = JSON . stringify ( json , null , 2 ) ; assetBindings . set ( join ( assetCatalog , 'Contents.json' ) , { contents } ) ; } ; const addFolderGroups = ( path , assetBindings , bundleId ) => { const components = path . split ( '/' ) ; if ( components . length === 0 ) { return ; } const json = makeFolderContentsJson ( bundleId ) ; const contents = JSON . stringify ( json , null , 2 ) ; let pathWalk = assetCatalog ; for ( const component of components ) { pathWalk = join ( pathWalk , component ) ; const contentsFile = join ( pathWalk , 'Contents.json' ) ; if ( assetBindings . has ( contentsFile ) ) { continue ; } assetBindings . set ( contentsFile , { contents } ) ; } } ; const addImageSetContents = ( destination , instance , assetBindings , bundleId ) => { const imageSetContents = join ( destination , 'Contents.json' ) ; const file = basename ( instance . file . src ) ; const file2x = basename ( instance . file2x . src ) ; const file3x = basename ( instance . file3x . src ) ; const json = makeImagesetContentsJson ( bundleId , file , file2x , file3x ) ; const contents = JSON . stringify ( json , null , 2 ) ; assetBindings . set ( imageSetContents , { contents } ) ; } ; const addImage = async ( file , destination , projectRoot , assetBindings ) => { const filename = basename ( file . src ) ; const path = join ( destination , filename ) ; assetBindings . set ( path , { contents : await getPathToFileContents ( join ( projectRoot , file . src ) , FileType . Image ) , copy : true , } ) ; } ; const addImageSet = async ( instance , projectRoot , assetBindings , bundleId ) => { const parsed1x = parse ( instance . file . src ) ; const destination = join ( assetCatalog , parsed1x . dir , `${ parsed1x . name }.imageset` ) ; return Promise . all ( [ addImage ( instance . file , destination , projectRoot , assetBindings ) , addImage ( instance . file2x , destination , projectRoot , assetBindings ) , addImage ( instance . file3x , destination , projectRoot , assetBindings ) , addImageSetContents ( destination , instance , assetBindings , bundleId ) , ] ) ; } ; const imageAssetBinder = async ( instance , { projectRoot , hot } , { assetBindings , bundleIdPrefix } ) => { if ( hot ) { return ; } assetBindings . delete ( instance . file . src ) ; assetBindings . delete ( instance . file2x . src ) ; assetBindings . delete ( instance . file3x . src ) ; const bundleId = `${ bundleIdPrefix }.Static` ; await Promise . all ( [ addCatalog ( assetBindings , bundleId ) , addFolderGroups ( dirname ( instance . file . src ) , assetBindings , bundleId ) , addImageSet ( instance , projectRoot , assetBindings , bundleId ) , ] ) ; } ; const binding = { sources : [ join ( sourcesPath , 'ios' , 'bindings' , 'Image+Binding.swift' ) ] , assetsBinder : imageAssetBinder , examples : [ { example : 'UIImage' , snippets : [ { lang : IosLanguages . Swift , templatePath : join ( examplesPath , 'UIImage.swift.handlebars' ) , } , { lang : IosLanguages . ObjectiveC , templatePath : join ( examplesPath , 'UIImage.objc.handlebars' ) , } , ] , } ] , } ; export = binding ;