win desktop icon + several small improvements

This commit is contained in:
Basile Burg 2015-06-05 02:24:14 +02:00
parent d448256765
commit 95536e0147
1 changed files with 37 additions and 46 deletions

View File

@ -1,13 +1,12 @@
module cesetup; module cesetup;
import std.stdio: writeln, readln; import std.stdio: writeln, readln;
import std.file: mkdir, exists; import std.file: mkdir, exists, remove, rmdir, getSize;
import std.getopt; import std.stream: File, FileMode;
import std.path;
import std.conv;
import std.string;
import std.process: environment, executeShell; import std.process: environment, executeShell;
import std.path: dirSeparator;
import std.string: strip, toLower;
import std.getopt;
version(X86) version(linux) version = nux32; version(X86) version(linux) version = nux32;
version(X86_64) version(linux) version = nux64; version(X86_64) version(linux) version = nux64;
@ -37,7 +36,7 @@ auto celic = Resource(cast(ubyte[]) import("coedit.license.txt"), "coedit.lice
auto dcdlic = Resource(cast(ubyte[]) import("dcd.license.txt"), "dcd.license.txt", false); auto dcdlic = Resource(cast(ubyte[]) import("dcd.license.txt"), "dcd.license.txt", false);
static string exePath, appDataPath; static string exePath, appDataPath, shortCutPath;
static bool asSu; static bool asSu;
static this() static this()
@ -46,6 +45,7 @@ static this()
{ {
exePath = environment.get("PROGRAMFILES") ~ r"\Coedit\"; exePath = environment.get("PROGRAMFILES") ~ r"\Coedit\";
appDataPath = environment.get("APPDATA") ~ r"\Coedit\"; appDataPath = environment.get("APPDATA") ~ r"\Coedit\";
shortCutPath = environment.get(r"USERPROFILE") ~ r"\Desktop\";
} }
else else
{ {
@ -54,11 +54,13 @@ static this()
{ {
exePath = "/usr/bin"; exePath = "/usr/bin";
appDataPath = "/home/" ~ environment.get("SUDO_USER") ~ "/Coedit/"; appDataPath = "/home/" ~ environment.get("SUDO_USER") ~ "/Coedit/";
shortCutPath = "/usr/share/applications/";
} }
else else
{ {
exePath = "/home/" ~ environment.get("USER") ~ "/bin/"; exePath = "/home/" ~ environment.get("USER") ~ "/bin/";
appDataPath = "/home/" ~ environment.get("USER") ~ "/Coedit/"; appDataPath = "/home/" ~ environment.get("USER") ~ "/Coedit/";
shortCutPath = "/home/" ~ environment.get("USER") ~ "/.local/share/applications/";
} }
} }
} }
@ -74,17 +76,26 @@ void main(string[] args)
); );
writeln("|---------------------------------------------|"); writeln("|---------------------------------------------|");
if (!uninstall)
writeln("| Coedit 1.0 RC1 setup |"); writeln("| Coedit 1.0 RC1 setup |");
else
writeln("| Coedit 1.0 RC1 uninstaller |");
writeln("|---------------------------------------------|"); writeln("|---------------------------------------------|");
version(win32) version(win32)
writeln("| the setup must be run as admin |"); writeln("| the setup program must be run as admin |");
else if(!asSu)
writeln("| the program can be setup globally (sudo) |");
writeln("| options: |"); writeln("| options: |");
writeln("| --nodcd: skip DCD setup |"); writeln("| --nodcd: skip DCD setup |");
writeln("| -u: uninstall |"); writeln("| -u: uninstall |");
writeln("| press a key to continue... |"); writeln("| press A to abort or another key to start... |");
writeln("|---------------------------------------------|"); writeln("|---------------------------------------------|");
readln; const string inp = readln.strip;
if (inp.toLower == "a") return;
writeln("|---------------------------------------------|"); writeln("|---------------------------------------------|");
size_t failures; size_t failures;
if(!uninstall) if(!uninstall)
@ -150,7 +161,6 @@ void main(string[] args)
version(win32) version(win32)
{ {
import std.file: rmdir;
try std.file.rmdir(exePath); try std.file.rmdir(exePath);
catch(Exception e) failures++; catch(Exception e) failures++;
} }
@ -173,9 +183,6 @@ void main(string[] args)
bool installResource(alias resource)(string path) bool installResource(alias resource)(string path)
{ {
import std.stream: File, FileMode;
import std.file: exists, mkdir;
if (!path.exists) if (!path.exists)
mkdir(path); mkdir(path);
if (!path.exists) if (!path.exists)
@ -183,7 +190,7 @@ bool installResource(alias resource)(string path)
try try
{ {
string fname = path ~ dirSeparator ~ resource.destName; const string fname = path ~ dirSeparator ~ resource.destName;
File f = new File(fname, FileMode.OutNew); File f = new File(fname, FileMode.OutNew);
f.write(resource.data); f.write(resource.data);
f.close; f.close;
@ -204,23 +211,16 @@ bool installResource(alias resource)(string path)
bool uninstallResource(alias resource)(string path) bool uninstallResource(alias resource)(string path)
{ {
import std.file: exists, remove;
string fname = path ~ dirSeparator ~ resource.destName; string fname = path ~ dirSeparator ~ resource.destName;
if (!fname.exists) return true; if (!fname.exists) return true;
try remove(fname); try remove(fname);
catch (Exception e) return false; catch (Exception e) return false;
return true; return true;
} }
void nuxPostInstall() void nuxPostInstall()
{ {
import std.stream: File, FileMode; File f = new File(shortCutPath ~ "coedit.desktop", FileMode.OutNew);
File f;
if (asSu) f = new File("/usr/share/applications/coedit.desktop", FileMode.OutNew);
else f = new File("/home/" ~ environment.get("USER") ~
"/.local/share/applications/coedit.desktop", FileMode.OutNew);
f.writeLine("[Desktop Entry]"); f.writeLine("[Desktop Entry]");
f.writeLine("Name=coedit"); f.writeLine("Name=coedit");
f.writeLine("Exec=coedit %f"); f.writeLine("Exec=coedit %f");
@ -233,28 +233,17 @@ void nuxPostInstall()
void nuxPostUninstall() void nuxPostUninstall()
{ {
import std.file: remove; try remove(shortCutPath ~ "coedit.desktop");
try
{
if (asSu)"/usr/share/applications/coedit.desktop".remove;
else remove("/home/" ~ environment.get("USER") ~
"/.local/share/applications/coedit.desktop");
}
catch (Exception e) {} catch (Exception e) {}
} }
void win32PostInstall() void win32PostInstall()
{ {
/* // notice: this is not a true shortcut, other options are
IconFile does not work // - create a true lnk by generating a vbs
*/ // - use winapi...
string link = environment.get(r"USERPROFILE") ~ "\\Desktop\\Coedit.url";
string target = exePath ~ "coedit.exe"; string target = exePath ~ "coedit.exe";
string ico = appDataPath ~ "coedit.ico"; File f = new File(shortCutPath ~ "Coedit.url", FileMode.OutNew);
import std.stream: File, FileMode;
File f = new File(link, FileMode.OutNew);
f.writeLine("[InternetShortcut]"); f.writeLine("[InternetShortcut]");
f.writeString("URL="); f.writeString("URL=");
f.writeLine("\"" ~ target ~ "\""); f.writeLine("\"" ~ target ~ "\"");
@ -266,4 +255,6 @@ void win32PostInstall()
void win32PostUninstall() void win32PostUninstall()
{ {
try remove(shortCutPath ~ "Coedit.url");
catch (Exception e) {}
} }