Skip to content

Instantly share code, notes, and snippets.

@chamberlainpi
Last active August 29, 2015 14:18
Show Gist options
  • Save chamberlainpi/654df52e429228b8e342 to your computer and use it in GitHub Desktop.
Save chamberlainpi/654df52e429228b8e342 to your computer and use it in GitHub Desktop.
Copy files in specific or common directory, useful for post-build processes.
package macros;
import sys.FileSystem;
import sys.io.File;
/**
* Can copy files in two ways:
* A) Provide only the first argument, where each elements specifies [sourceFile1, destFile1, sourceFile2, destFile2, ...], or;
* B) Provide a list of files to move (1st argument), and specify a common directory to copy them to;
*
* @usage:
* //These two files gets copied to 'MyApp', under the current user profile directory.
* --macro macros.CopyFiles.copy(["src_xml/config.xml", "bin/release.swf"], "{USERPROFILE}/MyApp/")
*
* @author Pierre Chamberlain
*/
class CopyFiles
{
static function copy(filesToCopy:Array<String>, ?commonOutputDir:String=null):Void {
var f:Int = 0;
function endsWith(str:String, char:String) {
return str.charAt(str.length - char.length) == char ? "" : char;
}
var curFile:String, newFile:String;
if(commonOutputDir==null) {
while (f < filesToCopy.length) {
curFile = filesToCopy[f];
newFile = filesToCopy[f + 1];
var newPopped:String = newFile.split("/").pop();
if (newPopped.lastIndexOf('.') == -1) {
newFile += endsWith(newFile, "/") + curFile.split("/").pop();
}
trace("Copying " + curFile + " to " + newFile);
File.copy(curFile, newFile);
f += 2;
}
} else {
var r:EReg = ~/{([a-z_ ]*)}/gi;
var m:Int = 0;
commonOutputDir = SysEnvTools.resolveEnvVars(commonOutputDir);
commonOutputDir += endsWith(commonOutputDir, "/");
for(curFile in filesToCopy) {
newFile = commonOutputDir + curFile.split("/").pop();
trace("Copying " + curFile + " to " + newFile);
File.copy(curFile, newFile);
}
}
}
}
package macros;
/**
* Helper for resolving paths containing System Environment Variables.
* @author Pierre Chamberlain
*/
class SysEnvTools {
public static function resolveEnvVars(paths:String, ?symbolUsed:String = "{}"):String {
var envAlphaNum = "([0-9a-z_ ]*)";
var pattern:String = switch (symbolUsed.length) {
case 2: symbolUsed.charAt(0) + envAlphaNum + symbolUsed.charAt(1);
case 1: symbolUsed + envAlphaNum + symbolUsed;
default: throw "There should only be 1, or 2 symbols passed as a start & end character.";
}
var r:EReg = new EReg(pattern, "gi");
return r.map(paths, function(reg):String {
return Sys.getEnv(r.matched(1));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment