44//DEPS io.quarkus:quarkus-picocli:2.1.0.CR1
55//DEPS org.zeroturnaround:zt-exec:1.12
66//FILES application.properties=../../../resources/application.properties
7+ //SOURCES YAMLDefaultProvider.java
78
89package dev .starfix ;
9- import io .quarkus .runtime .annotations .RegisterForReflection ;
1010import org .zeroturnaround .exec .ProcessExecutor ;
1111import org .zeroturnaround .exec .ProcessResult ;
1212import picocli .CommandLine ;
1313import picocli .CommandLine .Command ;
1414import picocli .CommandLine .ExitCode ;
1515import picocli .CommandLine .Parameters ;
1616
17+ import java .io .UnsupportedEncodingException ;
1718import java .io .BufferedReader ;
1819import java .io .IOException ;
1920import java .io .InputStreamReader ;
2021import java .net .URI ;
22+ import java .net .URLDecoder ;
2123import java .nio .file .Files ;
2224import java .nio .file .Path ;
2325import java .nio .file .Paths ;
2426import java .util .Arrays ;
2527import java .io .File ;
28+ import java .util .Properties ;
2629import java .util .concurrent .TimeoutException ;
2730import java .util .regex .Pattern ;
2831import com .fasterxml .jackson .databind .ObjectMapper ;
2932import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
3033
31- @ CommandLine .Command (mixinStandardHelpOptions = true )
34+ @ CommandLine .Command (name = "starfix" , mixinStandardHelpOptions = true , defaultValueProvider = YAMLDefaultProvider . class )
3235public class Starfix implements Runnable {
3336
3437 @ Parameters (arity = "0..1" )
3538 String uri ;
36-
39+
40+ // Holds path to destination Where the Repository Must Be Clonned
41+ @ CommandLine .Option (names = "--clone-path" , descriptionKey = "clone_path" )
42+ String clone_path ;
43+
44+ @ CommandLine .Option (names = "--editor" , descriptionKey = "ide" )
45+ String ide = "" ; // Holds command for IDE to open
46+
3747 @ Command
3848 public int config () throws Exception {
3949 editConfig ();
@@ -42,7 +52,6 @@ public int config() throws Exception {
4252
4353 @ Command (name = "clone" )
4454 public int cloneCmd (@ Parameters (index = "0" ) String url ) {
45-
4655 if (url .startsWith ("ide://" )) {
4756 // stripping out ide:// to simplify launcher scripts.
4857 url = url .substring (6 );
@@ -54,31 +63,13 @@ public int cloneCmd(@Parameters(index = "0") String url) {
5463 throw new IllegalArgumentException ("Not a valid URI for git repository" );
5564 }
5665
57- // Configuration identifiers
58- String clone_path = "" ;// Holds path to destination Where the Repository Must Be Clonned
59- String ide = "" ; // Holds command for IDE to open upon
60-
61- File configFile = getConfigFile ();// Calling functiono to fetch config file
62-
63- // Reading Configuration File
64- try {
65- if (!configFile .exists ()) {
66- // Incase no config file exists
67- editConfig (); // Calling function that lets user to configure
68- }
69-
70- // System.out.println("\nLoading configurations.....\n");
71- ObjectMapper mapper = new ObjectMapper (new YAMLFactory ());
72-
73- XConfig config = mapper .readValue (configFile , XConfig .class );
7466
75- ide = config .ide ;
76- clone_path = config .clone_path ;
77- if (ide == null || clone_path == null )
78- editConfig (); // Incase of absence of configuration in file Launch Config
67+ if (ide ==null ) {
68+ throw new IllegalArgumentException ("Editor not specifed, please specify via --editor or use config to set it up." );
69+ }
7970
80- } catch ( Exception e ) {
81- e . printStackTrace ( );
71+ if ( clone_path == null ) {
72+ throw new IllegalArgumentException ( "Clone path not specifed, please specify via --clone-path or use config to set it up." );
8273 }
8374
8475 try {
@@ -87,13 +78,12 @@ public int cloneCmd(@Parameters(index = "0") String url) {
8778 if (isBlob (url ))
8879 { // Example URL : https://github.com/starfixdev/starfix/blob/master/cli/pom.xml
8980 // Example URL2: https://github.com/hexsum/Mojo-Webqq/blob/master/script/check_dependencies.pl#L17
90-
9181 String temp = url .substring (url .indexOf ("blob/" )+5 );
9282 branch = temp .substring (0 ,temp .indexOf ("/" ));
9383 filePath = temp .substring (temp .indexOf ("/" )+1 );
84+ filePath = URLDecoder .decode (filePath ,"UTF-8" );
9485 url = url .substring (0 ,url .indexOf ("/blob" ));
9586 }
96-
9787 URI uri = new URI (url );
9888
9989 // extract name of repository
@@ -102,7 +92,6 @@ public int cloneCmd(@Parameters(index = "0") String url) {
10292
10393 String originUrl = url ;
10494 Path directory = Paths .get (clone_path , repo_name );
105-
10695 if (!Files .exists (directory )) // Check if the user cloned the repo previously and in that case no cloning is
10796 // needed
10897 gitClone (directory , originUrl );
@@ -112,7 +101,7 @@ public int cloneCmd(@Parameters(index = "0") String url) {
112101
113102 // Launching Editor on the Cloned Directory
114103 System .out .println ("Launching Editor Now..." );
115- launch_editor (directory , ide , directory .toAbsolutePath ().toString (),filePath );
104+ getIDE ( ide ). launch_editor (directory , ide , directory .toAbsolutePath ().toString (),filePath );
116105 } catch (Exception e ) {
117106 throw new IllegalStateException (e );
118107 }
@@ -159,12 +148,14 @@ public static File getConfigFile() {
159148
160149 // Function to edit configuration and serves for command line starfix config
161150 // editor
162- public static void editConfig () throws Exception {
151+ public void editConfig () throws Exception {
163152 System .out .println ("\n -------------------------------------------------------" );
164153 System .out .println ("\t \t Starfix Configuration Editor" );
165154 System .out .println ("-------------------------------------------------------\n " );
166- String clone_path = "" ;// Holds path to destination Where the Repository Must Be Clonned
167- String ide = "" ; // Holds command for IDE to open upon
155+ //String clone_path = "";// Holds path to destination Where the Repository Must Be Clonned
156+ //String ide = ""; // Holds command for IDE to open upon
157+ System .out .println ("IDE: " +ide );
158+ System .out .println ("Clone Path: " +clone_path );
168159
169160 File configFile = getConfigFile ();
170161
@@ -178,11 +169,11 @@ public static void editConfig() throws Exception {
178169 System .out .println (
179170 "\n -New configuration file will be created at: " + configFile .getAbsolutePath () + "\n " );
180171 configFile .createNewFile ();
181- }else {
172+ }/* else{
182173 XConfig config = new ObjectMapper(new YAMLFactory()).readValue(configFile, XConfig.class);
183174 ide = config.getIde();
184175 clone_path = config.getClone_path();
185- }
176+ }*/
186177
187178 // --------------------------First we'll input preferred IDE from
188179 // user------------------------------
@@ -234,7 +225,9 @@ public static void editConfig() throws Exception {
234225 }
235226 // ----------Now we'll write configurations to the YAML FILE------------
236227 ObjectMapper mapper = new ObjectMapper (new YAMLFactory ());
237- XConfig configuration = new XConfig (ide , clone_path );
228+ Properties configuration = new Properties ();
229+ configuration .put ("ide" , ide );
230+ configuration .put ("clone_path" , clone_path );
238231 mapper .writeValue (configFile , configuration ); // Writing to YAML File
239232
240233 } catch (Exception e ) {
@@ -246,13 +239,14 @@ public static void editConfig() throws Exception {
246239
247240 public static void gitClone (Path directory , String originUrl ) throws IOException , InterruptedException {
248241 // Function for git clonning
249- runCommand (directory .getParent (), "git" , "clone" , originUrl , directory .getFileName (). toString ());
242+ runCommand (directory .getParent (), "git" , "clone" , originUrl , directory .toString ());
250243 }
251244
252245 public static String runCommand (Path directory , String ... command ) throws IOException , InterruptedException {
253246 // Function to Run Commands using Process Builder
254247 ProcessResult presult ;
255248 try {
249+ System .out .println ("Running " + String .join (" " , command ));
256250 presult = new ProcessExecutor ().command (command ).redirectOutput (System .out ).redirectErrorStream (true ).readOutput (true )
257251 .execute ();
258252 } catch (TimeoutException e ) {
@@ -268,70 +262,80 @@ public static String runCommand(Path directory, String... command) throws IOExce
268262 return presult .outputUTF8 ();
269263 }
270264
271- @ RegisterForReflection
272- public static class XConfig {
273- public String ide ;
274- public String clone_path ;
275-
276- public XConfig () {
277- // Default constructor
278- }
279265
280- public XConfig (String ide , String clone_path ) {
281- this .ide = ide ;
282- this .clone_path = clone_path ;
266+ public static abstract class IDE {
267+ public abstract void launch_editor (Path directory , String ide , String path , String filePath )throws IOException , InterruptedException ;
283268
284- }
285-
286- public String getIde () {
287- return ide ;
288- }
289-
290- public void setIde (String ide ) {
291- this .ide = ide ;
292- }
293-
294- public String getClone_path () {
295- return clone_path ;
296- }
269+ }
297270
298- public void setClone_path (String clone_path ) {
299- this .clone_path = clone_path ;
271+ public static IDE getIDE (String ide ){
272+ // Will return IDE based on String
273+ switch (ide ){
274+ case "code" :
275+ case "code.cmd" :
276+ return new VsCode ();
277+
278+ case "idea" :
279+ case "idea64.exe" :
280+ return new IntelliJIdea ();
281+
282+ case "eclipse" :
283+ case "eclipse.exe" :
284+ return new Eclipse ();
285+ default :
286+ throw new IllegalArgumentException ("IDE not supported" );
300287 }
301288
302289 }
303290
304- public static void launch_editor (Path directory , String ide , String path , String filePath ) throws IOException , InterruptedException {
305-
306- if (filePath .indexOf ("#" )>0 ){
307- filePath = filePath .replace ("#L" ,"#" );
308- // ==== VS CODE ====
309- // code -g file:line
310- if (ide .equals ("code" )||ide .equals ("code.cmd" )){
291+ public static class VsCode extends IDE {
292+
293+ public void launch_editor (Path directory , String ide , String path , String filePath ) throws IOException , InterruptedException {
294+ if (filePath .indexOf ("#" )>0 ){
295+ // code -g file:line
296+ filePath = filePath .replace ("#L" ,"#" );
311297 filePath = filePath .replace ("#" ,":" );
312298 runCommand (directory .getParent (), ide ,"-g" ,path ,filePath );
313299 }
314- // ===== IntelliJ =====
315- // idea64.exe [--line <number>] [--column <number>] <path ...>
316- // idea /home/fahad/MyProjects/testings/jbang --line 3 /home/fahad/MyProjects/testings/jbang/otp.java
317- if (ide .equals ("idea" )||ide .equals ("idea.exe" )||ide .equals ("idea64.exe" )){
300+ else {
301+ runCommand (directory .getParent (),ide ,path ,filePath );
302+ }
303+ }
304+ }
305+
306+ public static class IntelliJIdea extends IDE {
307+
308+ public void launch_editor (Path directory , String ide , String path , String filePath ) throws IOException , InterruptedException {
309+ if (filePath .indexOf ("#" )>0 ){
310+ filePath = filePath .replace ("#L" ,"#" );
311+ // idea64.exe [--line <number>] [--column <number>] <path ...>
312+ // idea /home/fahad/MyProjects/testings/jbang --line 3 /home/fahad/MyProjects/testings/jbang/otp.java
318313 String lineNumber = filePath .substring (filePath .lastIndexOf ("#" )+1 );
319314 filePath = filePath .substring (0 ,filePath .lastIndexOf ("#" ));
320315 runCommand (directory .getParent (), ide ,path ,"--line" ,lineNumber ,filePath );
321316 }
322- // === Eclipse =====
323- // eclipse.exe file.txt:22
324- if (ide .equals ("eclipse" )){
325- filePath = filePath .replace ("#" ,":" );
326- runCommand (directory .getParent (), ide ,path ,filePath );
317+ else {
318+ runCommand (directory .getParent (),ide ,path ,filePath );
327319 }
328-
329- }
330- else {
331- runCommand (directory .getParent (),ide ,path ,filePath );// Launching the editor now
332320 }
333321 }
334322
323+ public static class Eclipse extends IDE {
335324
325+ public void launch_editor (Path directory , String ide , String path , String filePath ) throws IOException , InterruptedException {
326+ if (filePath .indexOf ("#" )>0 ){
327+ filePath = filePath .replace ("#L" ,"#" );
328+ // eclipse.exe file.txt:22
329+ if (ide .equals ("eclipse" )){
330+ filePath = filePath .replace ("#" ,":" );
331+ runCommand (directory .getParent (), ide ,path ,filePath );
332+ }
333+
334+ }
335+ else {
336+ runCommand (directory .getParent (),ide ,path ,filePath );
337+ }
338+ }
339+ }
336340
337341}
0 commit comments