import * as path from "path" ; import * as fs from "fs" ; import { FlrFileUtil } from "./FlrFileUtil" ; import * as glob from "glob" ; export class FlrAssetUtil { public static isAssetVariant ( file ) { if ( FlrFileUtil . isNonSvgImageResourceFile ( file ) ) { let parentDirPath = path . dirname ( file ) ; let parentDirName = path . basename ( parentDirPath ) ; let ratioRegex = /^((0\.[0-9]+)|([1-9]+[0-9]*(\.[0-9]+)?))[x]$/ ; if ( ratioRegex . test ( parentDirName ) ) { return true ; } } return false ; } public static isImageAsset ( asset ) { if ( FlrFileUtil . isNonSvgImageResourceFile ( asset ) ) { return true ; } return false ; } public static isPackageAsset ( asset ) { let packagePrefix = "packages/" ; if ( asset . startsWith ( packagePrefix ) ) { return true ; } return false ; } public static isSpecifiedPackageAsset ( packageName , asset ) { let specifiedPackagePrefix = "packages/" + packageName + "/" ; if ( asset . startsWith ( specifiedPackagePrefix ) ) { return true ; } return false ; } public static getMainResourceFile ( flutterProjectDir , packageName , asset ) { if ( this . isSpecifiedPackageAsset ( packageName , asset ) ) { let specifiedPackagePrefix = "packages/" + packageName + "/" ; let specifiedPackagePrefixRegex = new RegExp ( `^${ specifiedPackagePrefix }` ) ; let impliedRelativeResourceFile = asset . replace ( specifiedPackagePrefixRegex , "" ) ; impliedRelativeResourceFile = "lib/" + impliedRelativeResourceFile ; let mainResourceFile = flutterProjectDir + "/" + impliedRelativeResourceFile ; return mainResourceFile ; } else { let mainResourceFile = flutterProjectDir + "/" + asset ; return mainResourceFile ; } } public static isAssetExisted ( flutterProjectDir , packageName , asset ) { let mainResourceFile = this . getMainResourceFile ( flutterProjectDir , packageName , asset ) ; if ( FlrFileUtil . isNonSvgImageResourceFile ( mainResourceFile ) ) { if ( fs . existsSync ( mainResourceFile ) ) { return true ; } let fileBaseName = path . basename ( mainResourceFile ) ; let fileDir = path . dirname ( mainResourceFile ) ; var didFindVariantResourceFile = false ; let fileRegx = `${ fileDir }/*/${ fileBaseName }` ; glob . sync ( fileRegx ) . forEach ( ( file ) => { if ( this . isAssetVariant ( file ) ) { didFindVariantResourceFile = true ; } } ) ; if ( didFindVariantResourceFile ) { return true ; } } else { if ( fs . existsSync ( mainResourceFile ) ) { return true ; } } return false ; } public static generateMainAsset ( flutterProjectDir , packageName , legalResourceFile ) { let mainResourceFile = legalResourceFile ; if ( this . isAssetVariant ( legalResourceFile ) ) { let fileBasename = path . basename ( legalResourceFile ) ; let fileDir = path . dirname ( legalResourceFile ) ; let mainResourceFileDir = path . dirname ( fileDir ) ; mainResourceFile = `${ mainResourceFileDir }/${ fileBasename }` ; } let replaced = flutterProjectDir . replace ( /\\/g , "/" ) ; let flutterProjectDirPrefixRegex = new RegExp ( `^${ replaced }/` ) ; let mainRelativeResourceFile = mainResourceFile . replace ( flutterProjectDirPrefixRegex , "" ) ; let libPrefix = "lib/" ; if ( mainRelativeResourceFile . startsWith ( libPrefix ) ) { let libPrefixRegx = RegExp ( libPrefix ) ; let assetName = mainRelativeResourceFile . replace ( libPrefixRegx , "" ) ; let mainAsset = "packages/" + packageName + "/" + assetName ; return mainAsset ; } else { let assetName = mainRelativeResourceFile ; let mainAsset = assetName ; return mainAsset ; } } public static generateImageAssets ( flutterProjectDir , packageName , legalImageFileArray ) { var imageAssetSet = new Set ( ) ; legalImageFileArray . forEach ( ( legalResourceFile ) => { let imageAsset = this . generateMainAsset ( flutterProjectDir , packageName , legalResourceFile ) ; imageAssetSet . add ( imageAsset ) ; } ) ; let imageAssetArray = Array . from ( imageAssetSet . values ( ) ) ; return imageAssetArray ; } public static generateTextAssets ( flutterProjectDir , packageName , legalTextFileArray ) { var textAssetSet = new Set ( ) ; legalTextFileArray . forEach ( ( legalResourceFile ) => { let textAsset = this . generateMainAsset ( flutterProjectDir , packageName , legalResourceFile ) ; textAssetSet . add ( textAsset ) ; } ) ; let textAssetArray = Array . from ( textAssetSet . values ( ) ) ; return textAssetArray ; } public static generateFontAssetConfigs ( flutterProjectDir , packageName , legalFontFileArray ) { var fontAssetConfigArray = new Array ( ) ; legalFontFileArray . forEach ( ( legalResourceFile ) => { let fontAsset = this . generateMainAsset ( flutterProjectDir , packageName , legalResourceFile ) ; let fontAssetConfig = { asset : fontAsset , } ; fontAssetConfigArray . push ( fontAssetConfig ) ; } ) ; return fontAssetConfigArray ; } public static mergeFlutterAssets ( flutterProjectDir , packageName , newAssetArray , oldAssetArray ) { var legalOldAssetArray = new Array ( ) ; var diffAssetArray = oldAssetArray . filter ( ( asset ) => { return ! newAssetArray . includes ( asset ) ; } ) ; diffAssetArray . forEach ( ( asset ) => { if ( this . isPackageAsset ( asset ) ) { if ( ! this . isSpecifiedPackageAsset ( packageName , asset ) ) { legalOldAssetArray . push ( asset ) ; return ; } } if ( this . isAssetExisted ( flutterProjectDir , packageName , asset ) ) { legalOldAssetArray . push ( asset ) ; return ; } } ) ; legalOldAssetArray . sort ( ) ; let mergedAssetArray = newAssetArray . concat ( legalOldAssetArray ) ; return mergedAssetArray ; } }