package utils; import helpers.FileHelper; import helpers.LogHelper; import helpers.PathHelper; import project.Haxelib; import project.HXProject; import sys.FileSystem; class CreateTemplate { public static function createExtension (words:Array , userDefines:Map):Void { var title = "SampleExtension"; if (words.length > 1) { title = words[1]; } var file = StringTools.replace (title, " ", ""); var extension = StringTools.replace (file, "-", "_"); var className = extension.substr (0, 1).toUpperCase () + extension.substr (1); var context:Dynamic = { }; context.file = file; context.extension = extension; context.className = className; context.extensionLowerCase = extension.toLowerCase (); context.extensionUpperCase = extension.toUpperCase (); context.ANDROID_TARGET_SDK_VERSION = "::ANDROID_TARGET_SDK_VERSION::"; PathHelper.mkdir (title); FileHelper.recursiveCopyTemplate ([ PathHelper.getHaxelib (new Haxelib ("lime"), true) + "/templates" ], "extension", title, context); if (FileSystem.exists (title + "/Extension.hx")) { FileSystem.rename (title + "/Extension.hx", title + "/" + className + ".hx"); } if (FileSystem.exists (title + "/project/common/Extension.cpp")) { FileSystem.rename (title + "/project/common/Extension.cpp", title + "/project/common/" + file + ".cpp"); } if (FileSystem.exists (title + "/project/include/Extension.h")) { FileSystem.rename (title + "/project/include/Extension.h", title + "/project/include/" + file + ".h"); } if (FileSystem.exists (title + "/dependencies/android/src/org/haxe/extension/Extension.java")) { FileSystem.rename (title + "/dependencies/android/src/org/haxe/extension/Extension.java", title + "/dependencies/android/src/org/haxe/extension/" + file + ".java"); } if (FileSystem.exists (title)) { PathHelper.mkdir (title + "/ndll"); PathHelper.mkdir (title + "/ndll/Linux"); PathHelper.mkdir (title + "/ndll/Linux64"); PathHelper.mkdir (title + "/ndll/Mac"); PathHelper.mkdir (title + "/ndll/Mac64"); PathHelper.mkdir (title + "/ndll/Windows"); } } public static function createProject (words:Array , userDefines:Map):Void { var colonIndex = words[0].indexOf (":"); var projectName = null; var sampleName = null; var outputName = "SampleProject"; if (colonIndex == -1) { projectName = words[0]; if (words.length > 1) { sampleName = words[1]; } if (words.length > 2) { outputName = words[2]; } } else { projectName = words[0].substring (0, colonIndex); sampleName = words[0].substr (colonIndex + 1); if (words.length > 1) { outputName = words[1]; } } if (projectName == "project") { projectName = CommandLineTools.defaultLibrary; if (sampleName != null) { outputName = sampleName; } } if (projectName == null || projectName == "") { projectName = CommandLineTools.defaultLibrary; } if (projectName != null && projectName != "") { var defines = new Map (); defines.set ("create", 1); var project = HXProject.fromHaxelib (new Haxelib (projectName), defines); if (project != null) { var id = [ "com", "sample", "project" ]; /*if (colonIndex != -1 && words.length > 1 || ) { var name = words[1]; name = new EReg ("[^a-zA-Z0-9.]", "g").replace (name, ""); id = name.split ("."); if (id.length < 3) { id = [ "com", "example" ].concat (id); } }*/ var company = "Company Name"; /*if (words.length > 2) { company = words[2]; }*/ var context:Dynamic = { }; var name = outputName; //var name = words[1].split (".").pop (); var alphaNumeric = new EReg ("[a-zA-Z0-9]", "g"); var title = ""; var capitalizeNext = true; for (i in 0...name.length) { if (alphaNumeric.match (name.charAt (i))) { if (capitalizeNext) { title += name.charAt (i).toUpperCase (); } else { title += name.charAt (i); } capitalizeNext = false; } else { capitalizeNext = true; } } var packageName = id.join (".").toLowerCase (); context.title = title; context.packageName = packageName; context.version = "1.0.0"; context.company = company; context.file = StringTools.replace (title, " ", ""); for (define in userDefines.keys ()) { Reflect.setField (context, define, userDefines.get (define)); } var folder = name; if (colonIndex > -1) { if (words.length > 1) { folder = PathHelper.tryFullPath (words[1]); } } else { if (words.length > 2) { folder = PathHelper.tryFullPath (words[2]); } } /*if (words.length > 2) { folder = PathHelper.tryFullPath (words[2]); }*/ PathHelper.mkdir (folder); FileHelper.recursiveCopyTemplate (project.templatePaths, "project", folder, context); try { if (FileSystem.exists (folder + "/Project.hxproj")) { if (FileSystem.exists (folder + "/" + title + ".hxproj")) { FileSystem.deleteFile (folder + "/" + title + ".hxproj"); } FileSystem.rename (folder + "/Project.hxproj", folder + "/" + title + ".hxproj"); } } catch (e:Dynamic) {} return; } } LogHelper.error ("Could not find project \"" + projectName + "\""); } public static function createSample (words:Array , userDefines:Map) { var colonIndex = words[0].indexOf (":"); var projectName = null; var sampleName = null; var outputPath = null; if (colonIndex == -1 && words.length > 1) { projectName = words[0]; sampleName = words[1]; if (words.length > 2) { outputPath = words[2]; } } else { projectName = words[0].substring (0, colonIndex); sampleName = words[0].substr (colonIndex + 1); if (words.length > 1) { outputPath = words[1]; } } if (projectName == null || projectName == "") { projectName = CommandLineTools.defaultLibrary; } if (sampleName == null || sampleName == "") { LogHelper.error ("You must specify a sample name to copy when using \"" + CommandLineTools.commandName + " create\""); return; } var defines = new Map (); defines.set ("create", 1); var project = HXProject.fromHaxelib (new Haxelib (projectName), defines); if (project == null && outputPath == null) { outputPath = sampleName; sampleName = projectName; projectName = CommandLineTools.defaultLibrary; project = HXProject.fromHaxelib (new Haxelib (projectName), defines); } if (project != null) { if (outputPath == null) { outputPath = sampleName; } var samplePaths = project.samplePaths.copy (); samplePaths.reverse (); for (samplePath in samplePaths) { var sourcePath = PathHelper.combine (samplePath, sampleName); if (FileSystem.exists (sourcePath)) { PathHelper.mkdir (outputPath); FileHelper.recursiveCopy (sourcePath, PathHelper.tryFullPath (outputPath)); return; } } } LogHelper.error ("Could not find sample \"" + sampleName + "\" in project \"" + projectName + "\""); } public static function listSamples (projectName:String, userDefines:Map) { var templates = []; if (projectName != null && projectName != "") { var defines = new Map (); defines.set ("create", 1); var project = HXProject.fromHaxelib (new Haxelib (projectName), defines); if (project != null) { var samplePaths = project.samplePaths.copy (); if (samplePaths.length > 0) { samplePaths.reverse (); for (samplePath in samplePaths) { var path = PathHelper.tryFullPath (samplePath); for (name in FileSystem.readDirectory (path)) { if (!StringTools.startsWith (name, ".") && FileSystem.isDirectory (path + "/" + name)) { templates.push (name); } } } } /*templates.push ("extension"); var projectTemplate = PathHelper.findTemplate (project.templatePaths, "project", false); if (projectTemplate != null) { templates.push ("project"); }*/ } } if (templates.length == 0) { projectName = CommandLineTools.defaultLibrary; } LogHelper.println ("\x1b[1mYou must specify a template when using the 'create' command.\x1b[0m"); LogHelper.println (""); LogHelper.println (" " + LogHelper.accentColor + "Usage:\x1b[0m \x1b[1m" + CommandLineTools.commandName + "\x1b[0m create extension \"ExtensionName\""); LogHelper.println (" " + LogHelper.accentColor + "Usage:\x1b[0m \x1b[1m" + CommandLineTools.commandName + "\x1b[0m create " + projectName + " project \x1b[3;37m\"OutputDirectory\"\x1b[0m"); LogHelper.println (" " + LogHelper.accentColor + "Usage:\x1b[0m \x1b[1m" + CommandLineTools.commandName + "\x1b[0m create " + projectName + " (sample) \x1b[3;37m\"OutputDirectory\"\x1b[0m"); if (templates.length > 0) { LogHelper.println (""); LogHelper.println (" " + LogHelper.accentColor + "Available Samples:\x1b[0m"); LogHelper.println (""); for (template in templates) { Sys.println (" * " + template); } } Sys.println (""); } }