44import com .laytonsmith .PureUtilities .CommandExecutor ;
55import com .laytonsmith .PureUtilities .Common .FileUtil ;
66import com .laytonsmith .PureUtilities .Common .MutableObject ;
7+ import com .laytonsmith .PureUtilities .Common .OSUtils ;
78import com .laytonsmith .PureUtilities .Common .StreamUtils ;
89import com .laytonsmith .PureUtilities .Common .StringUtils ;
910import com .laytonsmith .PureUtilities .Common .WinRegistry ;
@@ -846,66 +847,71 @@ public boolean doBuiltin(String script) {
846847 }
847848
848849 public static void install () {
849- if (TermColors .SYSTEM == TermColors .SYS .UNIX ) {
850- try {
851- URL jar = Interpreter .class .getProtectionDomain ().getCodeSource ().getLocation ();
852- File exe = new File (INTERPRETER_INSTALLATION_LOCATION );
853- String bashScript = Static .GetStringResource ("/interpreter-helpers/bash.sh" );
850+ if (null == OSUtils .GetOS ()) {
851+ StreamUtils .GetSystemErr ().println ("Cmdline MethodScript is only supported on Unix and Windows" );
852+ return ;
853+ } else switch (OSUtils .GetOS ()) {
854+ case LINUX :
855+ case MAC :
854856 try {
855- bashScript = bashScript .replaceAll ("%%LOCATION%%" , jar .toURI ().getPath ());
856- } catch (URISyntaxException ex ) {
857- ex .printStackTrace ();
858- }
859- exe .createNewFile ();
860- if (!exe .canWrite ()) {
861- throw new IOException ();
862- }
863- FileUtil .write (bashScript , exe );
864- exe .setExecutable (true , false );
865- File manDir = new File ("/usr/local/man/man1" );
866- if (manDir .exists ()) {
867- //Don't do this installation if the man pages aren't already there.
868- String manPage = Static .GetStringResource ("/interpreter-helpers/manpage" );
857+ URL jar = Interpreter .class .getProtectionDomain ().getCodeSource ().getLocation ();
858+ File exe = new File (INTERPRETER_INSTALLATION_LOCATION );
859+ String bashScript = Static .GetStringResource ("/interpreter-helpers/bash.sh" );
869860 try {
870- manPage = DocGenTemplates .DoTemplateReplacement (manPage , DocGenTemplates .GetGenerators ());
871- File manPageFile = new File (manDir , "mscript.1" );
872- FileUtil .write (manPage , manPageFile );
873- } catch (DocGenTemplates .Generator .GenerateException ex ) {
874- Logger .getLogger (Interpreter .class .getName ()).log (Level .SEVERE , null , ex );
861+ bashScript = bashScript .replaceAll ("%%LOCATION%%" , jar .toURI ().getPath ());
862+ } catch (URISyntaxException ex ) {
863+ ex .printStackTrace ();
875864 }
876- }
877- } catch (IOException e ) {
878- StreamUtils .GetSystemErr ().println ("Cannot install. You must run the command with sudo for it to succeed, however, did you do that?" );
879- return ;
880- }
881- } else {
882- Path tmp = null ;
883- try {
884- // 1. Unpack the csharp installer program in a temporary directory
885- File root = new File (Interpreter .class .getResource ("/interpreter-helpers/csharp" ).toExternalForm ());
886- ZipReader zReader = new ZipReader (root );
887- tmp = Files .createTempDirectory ("methodscript-installer" , new FileAttribute []{});
888- zReader .recursiveCopy (tmp .toFile (), false );
889-
890- // 2. Write the location of this jar to the registry
891- String me = ClassDiscovery .GetClassContainer (Interpreter .class ).toExternalForm ().substring (6 );
892- String keyName = "Software\\ MethodScript" ;
893- WinRegistry .createKey (WinRegistry .HKEY_CURRENT_USER , keyName );
894- WinRegistry .writeStringValue (WinRegistry .HKEY_CURRENT_USER , keyName , "JarLocation" , me );
895-
896- // 3. Execute the setup.exe file
897- File setup = new File (tmp .toFile (), "setup.exe" );
898- int setupResult = new CommandExecutor (new String []{setup .getAbsolutePath ()}).start ().waitFor ();
899- if (setupResult != 0 ) {
900- StreamUtils .GetSystemErr ().println ("Setup failed to complete successfully (exit code " + setupResult + ")" );
901- System .exit (setupResult );
902- } else {
903- StreamUtils .GetSystemOut ().println ("Setup has begun. Finish the installation in the GUI." );
904- }
905- } catch (IOException | InterruptedException | IllegalAccessException | InvocationTargetException ex ) {
906- ex .printStackTrace (StreamUtils .GetSystemErr ());
907- System .exit (1 );
908- }
865+ exe .createNewFile ();
866+ if (!exe .canWrite ()) {
867+ throw new IOException ();
868+ }
869+ FileUtil .write (bashScript , exe );
870+ exe .setExecutable (true , false );
871+ File manDir = new File ("/usr/local/man/man1" );
872+ if (manDir .exists ()) {
873+ //Don't do this installation if the man pages aren't already there.
874+ String manPage = Static .GetStringResource ("/interpreter-helpers/manpage" );
875+ try {
876+ manPage = DocGenTemplates .DoTemplateReplacement (manPage , DocGenTemplates .GetGenerators ());
877+ File manPageFile = new File (manDir , "mscript.1" );
878+ FileUtil .write (manPage , manPageFile );
879+ } catch (DocGenTemplates .Generator .GenerateException ex ) {
880+ Logger .getLogger (Interpreter .class .getName ()).log (Level .SEVERE , null , ex );
881+ }
882+ }
883+ } catch (IOException e ) {
884+ StreamUtils .GetSystemErr ().println ("Cannot install. You must run the command with sudo for it to succeed, however, did you do that?" );
885+ return ;
886+ } break ;
887+ case WINDOWS :
888+ Path tmp = null ;
889+ try {
890+ // 1. Unpack the csharp installer program in a temporary directory
891+ File root = new File (Interpreter .class .getResource ("/interpreter-helpers/csharp" ).toExternalForm ());
892+ ZipReader zReader = new ZipReader (root );
893+ tmp = Files .createTempDirectory ("methodscript-installer" , new FileAttribute []{});
894+ zReader .recursiveCopy (tmp .toFile (), false );
895+
896+ // 2. Write the location of this jar to the registry
897+ String me = ClassDiscovery .GetClassContainer (Interpreter .class ).toExternalForm ().substring (6 );
898+ String keyName = "Software\\ MethodScript" ;
899+ WinRegistry .createKey (WinRegistry .HKEY_CURRENT_USER , keyName );
900+ WinRegistry .writeStringValue (WinRegistry .HKEY_CURRENT_USER , keyName , "JarLocation" , me );
901+
902+ // 3. Execute the setup.exe file
903+ File setup = new File (tmp .toFile (), "setup.exe" );
904+ int setupResult = new CommandExecutor (new String []{setup .getAbsolutePath ()}).start ().waitFor ();
905+ if (setupResult != 0 ) {
906+ StreamUtils .GetSystemErr ().println ("Setup failed to complete successfully (exit code " + setupResult + ")" );
907+ System .exit (setupResult );
908+ } else {
909+ StreamUtils .GetSystemOut ().println ("Setup has begun. Finish the installation in the GUI." );
910+ }
911+ } catch (IOException | InterruptedException | IllegalAccessException | InvocationTargetException ex ) {
912+ ex .printStackTrace (StreamUtils .GetSystemErr ());
913+ System .exit (1 );
914+ } break ;
909915 }
910916 StreamUtils .GetSystemOut ().println ("MethodScript has successfully been installed on your system. Note that you may need to rerun the install command"
911917 + " if you change locations of the jar, or rename it. Be sure to put \" #!" + INTERPRETER_INSTALLATION_LOCATION + "\" at the top of all your scripts,"
@@ -915,19 +921,27 @@ public static void install() {
915921 }
916922
917923 public static void uninstall () {
918- if (TermColors .SYSTEM == TermColors .SYS .UNIX ) {
919- try {
920- File exe = new File (INTERPRETER_INSTALLATION_LOCATION );
921- if (!exe .delete ()) {
922- throw new IOException ();
923- }
924- } catch (IOException e ) {
925- StreamUtils .GetSystemErr ().println ("Cannot uninstall. You must run the command with sudo for it to succeed, however, did you do that?" );
926- return ;
927- }
928- } else {
924+ if (null == OSUtils .GetOS ()) {
929925 StreamUtils .GetSystemErr ().println ("Sorry, cmdline functionality is currently only supported on unix systems! Check back soon though!" );
930926 return ;
927+ } else switch (OSUtils .GetOS ()) {
928+ case LINUX :
929+ case MAC :
930+ try {
931+ File exe = new File (INTERPRETER_INSTALLATION_LOCATION );
932+ if (!exe .delete ()) {
933+ throw new IOException ();
934+ }
935+ } catch (IOException e ) {
936+ StreamUtils .GetSystemErr ().println ("Cannot uninstall. You must run the command with sudo for it to succeed, however, did you do that?" );
937+ return ;
938+ } break ;
939+ case WINDOWS :
940+ StreamUtils .GetSystemOut ().println ("To uninstall on windows, please uninstall from the Add or Remove Programs application." );
941+ return ;
942+ default :
943+ StreamUtils .GetSystemErr ().println ("Sorry, cmdline functionality is currently only supported on unix systems! Check back soon though!" );
944+ return ;
931945 }
932946 StreamUtils .GetSystemOut ().println ("MethodScript has been uninstalled from this system." );
933947 }
0 commit comments