| 1 |
/+ |
|---|
| 2 |
Copyright (c) 2006 Eric Anderton |
|---|
| 3 |
Modifications made by Daniel Keep |
|---|
| 4 |
|
|---|
| 5 |
Permission is hereby granted, free of charge, to any person |
|---|
| 6 |
obtaining a copy of this software and associated documentation |
|---|
| 7 |
files (the "Software"), to deal in the Software without |
|---|
| 8 |
restriction, including without limitation the rights to use, |
|---|
| 9 |
copy, modify, merge, publish, distribute, sublicense, and/or |
|---|
| 10 |
sell copies of the Software, and to permit persons to whom the |
|---|
| 11 |
Software is furnished to do so, subject to the following |
|---|
| 12 |
conditions: |
|---|
| 13 |
|
|---|
| 14 |
The above copyright notice and this permission notice shall be |
|---|
| 15 |
included in all copies or substantial portions of the Software. |
|---|
| 16 |
|
|---|
| 17 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 18 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|---|
| 19 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 20 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|---|
| 21 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 22 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|---|
| 23 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|---|
| 24 |
OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 25 |
+/ |
|---|
| 26 |
module util.script; |
|---|
| 27 |
|
|---|
| 28 |
public import std.file; |
|---|
| 29 |
public import std.format; |
|---|
| 30 |
public import std.path; |
|---|
| 31 |
public import std.process; |
|---|
| 32 |
public import std.stdio; |
|---|
| 33 |
public import std.string; |
|---|
| 34 |
public import std.zip; |
|---|
| 35 |
public import std.c.stdio; |
|---|
| 36 |
|
|---|
| 37 |
version(Windows) {} |
|---|
| 38 |
else version(linux) {} |
|---|
| 39 |
else |
|---|
| 40 |
{ |
|---|
| 41 |
pragma(msg, "Sorry; your platform is not supported by the script |
|---|
| 42 |
library."); |
|---|
| 43 |
static assert(0); |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
// NOTE: the odd use of templates here is to force the template instances to |
|---|
| 47 |
// compile into the referencing module. This way, scripts built on top of |
|---|
| 48 |
// these commands can be created w/o need for the 'build' tool. |
|---|
| 49 |
|
|---|
| 50 |
// Simply mixin the Script template to use these functions in your .ds files |
|---|
| 51 |
|
|---|
| 52 |
template Script() |
|---|
| 53 |
{ |
|---|
| 54 |
bool verboseCommands = false; |
|---|
| 55 |
|
|---|
| 56 |
version(Windows) |
|---|
| 57 |
{ |
|---|
| 58 |
const EXEFILE = ".exe"; |
|---|
| 59 |
const OBJFILE = ".obj"; |
|---|
| 60 |
const LIBFILE = ".lib"; |
|---|
| 61 |
const DLLFILE = ".dll"; |
|---|
| 62 |
} |
|---|
| 63 |
version(linux) |
|---|
| 64 |
{ |
|---|
| 65 |
const EXEFILE = ""; |
|---|
| 66 |
const OBJFILE = ".o"; |
|---|
| 67 |
const LIBFILE = ".a"; |
|---|
| 68 |
const DLLFILE = ".so"; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
int |
|---|
| 72 |
exec(char[] pathname, char[][] params ...) |
|---|
| 73 |
{ |
|---|
| 74 |
char[] command = escape(fixPath(pathname)); |
|---|
| 75 |
foreach(str; params) command ~= " " ~ escape(str); |
|---|
| 76 |
if( verboseCommands ) echo(command); |
|---|
| 77 |
system(command); |
|---|
| 78 |
return 0; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
void |
|---|
| 82 |
echo(char[][] params ...) |
|---|
| 83 |
{ |
|---|
| 84 |
foreach( param ; params ) writef("%s", param); |
|---|
| 85 |
writefln(""); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
/+void |
|---|
| 89 |
echof(...) |
|---|
| 90 |
{ |
|---|
| 91 |
// D really needs argument exploding... |
|---|
| 92 |
writefx(stdout, _arguments, _argptr, true); |
|---|
| 93 |
}+/ |
|---|
| 94 |
|
|---|
| 95 |
void |
|---|
| 96 |
echof(T...)(T args) |
|---|
| 97 |
{ |
|---|
| 98 |
writefln("", args); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
char[] |
|---|
| 102 |
escape(char[] string) |
|---|
| 103 |
{ |
|---|
| 104 |
// This is really dumb at the moment; it doesn't handle escaping |
|---|
| 105 |
// quotes, etc. Really need to write a proper implementation... |
|---|
| 106 |
if( string.find(' ') != -1 ) |
|---|
| 107 |
return "\"" ~ string ~ "\""; |
|---|
| 108 |
else |
|---|
| 109 |
return string; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
char[] |
|---|
| 113 |
fixPath(char[] path) |
|---|
| 114 |
{ |
|---|
| 115 |
version(Windows) return std.string.replace(path,"/","\\"); |
|---|
| 116 |
version(linux) return std.string.replace(path,"\\","/"); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
void |
|---|
| 120 |
build(char[][] options ...) |
|---|
| 121 |
{ |
|---|
| 122 |
char[][] opts; |
|---|
| 123 |
foreach( opt ; options ) |
|---|
| 124 |
{ |
|---|
| 125 |
opts.length = opts.length + 1; |
|---|
| 126 |
opts[$-1] = fixPath(opt); |
|---|
| 127 |
} |
|---|
| 128 |
exec("bud",opts); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
void |
|---|
| 132 |
copyFile(char[] src, char[] dest) |
|---|
| 133 |
{ |
|---|
| 134 |
version(Windows) exec("copy", fixPath(src), fixPath(dest)); |
|---|
| 135 |
version(linux) exec("cp", fixPath(src), fixPath(dest)); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
void |
|---|
| 139 |
removeFile(char[] src) |
|---|
| 140 |
{ |
|---|
| 141 |
version(Windows) exec("del", fixPath(src)); |
|---|
| 142 |
version(linux) exec("rm", fixPath(src)); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
void |
|---|
| 146 |
moveFile(char[] src, char[] dest) |
|---|
| 147 |
{ |
|---|
| 148 |
version(Windows) exec("move", fixPath(src), fixPath(dest)); |
|---|
| 149 |
version(linux) exec("mv", fixPath(src), fixPath(dest)); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
char[] |
|---|
| 153 |
path(char[][] parts ...) |
|---|
| 154 |
{ |
|---|
| 155 |
char[] result = ""; |
|---|
| 156 |
if( parts.length > 0 ) |
|---|
| 157 |
result = parts[0]; |
|---|
| 158 |
for( uint i=1; i<parts.length; i++ ) |
|---|
| 159 |
result = std.path.join(result, parts[i]); |
|---|
| 160 |
return fixPath(result); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
void |
|---|
| 164 |
zip(char[] dest, char[][] targetFiles ...) |
|---|
| 165 |
{ |
|---|
| 166 |
char[] destPath = fixPath(dest); |
|---|
| 167 |
ZipArchive archive; |
|---|
| 168 |
|
|---|
| 169 |
if( std.file.exists(destPath) ) |
|---|
| 170 |
{ |
|---|
| 171 |
archive = new ZipArchive(std.file.read(destPath)); |
|---|
| 172 |
} |
|---|
| 173 |
else |
|---|
| 174 |
{ |
|---|
| 175 |
archive = new ZipArchive(); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
foreach( filename; targetFiles ) |
|---|
| 179 |
{ |
|---|
| 180 |
auto member = new ArchiveMember(); |
|---|
| 181 |
auto name = fixPath(filename); |
|---|
| 182 |
if( verboseCommands ) echof("Adding: %s", name); |
|---|
| 183 |
member.expandedData = cast(ubyte[]) std.file.read(name); |
|---|
| 184 |
member.name = name; |
|---|
| 185 |
archive.addMember(member); |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
std.file.write(destPath, archive.build()); |
|---|
| 189 |
if( verboseCommands ) echof("Created Zip: %s",destPath); |
|---|
| 190 |
} |
|---|
| 191 |
} |
|---|