diff --git a/source/app.d b/source/app.d index 8021dbf..b22c236 100644 --- a/source/app.d +++ b/source/app.d @@ -122,9 +122,9 @@ showHelp(cmd); return 0; case "init": - string dir = "."; + string dir; if( args.length >= 2 ) dir = args[1]; - initDirectory(dir); + dub.createEmptyPackage(Path(dir)); return 0; case "upgrade": dub.loadPackageFromCwd(); @@ -290,73 +290,3 @@ `); } - -private void initDirectory(string fName) -{ - Path cwd; - //Check to see if a target directory is specified. - if(fName != ".") { - if(!existsFile(fName)) - createDirectory(fName); - cwd = Path(fName); - } - //Otherwise use the current directory. - else - cwd = Path("."); - - //Make sure we do not overwrite anything accidentally - if( (existsFile(cwd ~ "package.json")) || - (existsFile(cwd ~ "source" )) || - (existsFile(cwd ~ "views" )) || - (existsFile(cwd ~ "public" ))) - { - logInfo("The current directory is not empty.\n" - "vibe init aborted."); - //Exit Immediately. - return; - } - - //raw strings must be unindented. - immutable packageJson = -`{ - "name": "`~(fName == "." ? "my-project" : fName)~`", - "version": "0.0.1", - "description": "An example project skeleton", - "homepage": "http://example.org", - "copyright": "Copyright © 2000, Edit Me", - "authors": [ - "Your Name" - ], - "dependencies": { - } -} -`; - immutable appFile = -`import std.stdio; - -void main() -{ - writeln("Edit source/app.d to start your project."); -} -`; - //Make sure we do not overwrite anything accidentally - if( (existsFile(cwd ~ PackageJsonFilename)) || - (existsFile(cwd ~ "source" )) || - (existsFile(cwd ~ "views" )) || - (existsFile(cwd ~ "public" ))) - { - logInfo("The current directory is not empty.\n" - "vibe init aborted."); - //Exit Immediately. - return; - } - //Create the common directories. - createDirectory(cwd ~ "source"); - createDirectory(cwd ~ "views" ); - createDirectory(cwd ~ "public"); - //Create the common files. - openFile(cwd ~ PackageJsonFilename, FileMode.Append).write(packageJson); - openFile(cwd ~ "source/app.d", FileMode.Append).write(appFile); - //Act smug to the user. - logInfo("Successfully created empty project."); -} diff --git a/source/dub/dub.d b/source/dub/dub.d index 783e57f..5d9a91a 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -227,4 +227,59 @@ if( !abs_path.absolute ) abs_path = m_cwd ~ abs_path; m_packageManager.removeLocalPackage(abs_path, system ? LocalPackageType.system : LocalPackageType.user); } + + void createEmptyPackage(Path path) + { + path.normalize(); + + //Check to see if a target directory needs to be created + if( !path.empty ){ + if( !existsFile(path) ) + createDirectory(path); + } + + //Make sure we do not overwrite anything accidentally + if( existsFile(path ~ PackageJsonFilename) || + existsFile(path ~ "source") || + existsFile(path ~ "views") || + existsFile(path ~ "public") ) + { + throw new Exception("The current directory is not empty.\n"); + } + + //raw strings must be unindented. + immutable packageJson = +`{ + "name": "`~(path.empty ? "my-project" : path.head.toString())~`", + "description": "An example project skeleton", + "homepage": "http://example.org", + "copyright": "Copyright © 2000, Your Name", + "authors": [ + "Your Name" + ], + "dependencies": { + } +} +`; + immutable appFile = +`import std.stdio; + +void main() +{ + writeln("Edit source/app.d to start your project."); +} +`; + + //Create the common directories. + createDirectory(path ~ "source"); + createDirectory(path ~ "views"); + createDirectory(path ~ "public"); + + //Create the common files. + openFile(path ~ PackageJsonFilename, FileMode.Append).write(packageJson); + openFile(path ~ "source/app.d", FileMode.Append).write(appFile); + + //Act smug to the user. + logInfo("Successfully created an empty project in '"~path.toNativeString()~"'."); + } }