1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.cicd
;
13 import java
.io
.IOException
;
14 import java
.nio
.file
.Files
;
15 import java
.nio
.file
.Path
;
16 import java
.nio
.file
.Paths
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Arrays
;
19 import java
.util
.List
;
20 import java
.util
.regex
.Pattern
;
23 * Access to the Fossil SCM command line interface.
27 public class FossilCommand
29 /** The executable path. */
30 protected final Path exe
;
33 * Initializes the command executor.
35 * @param __exe The executable to use.
36 * @throws NullPointerException On null arguments.
39 public FossilCommand(Path __exe
)
40 throws NullPointerException
43 throw new NullPointerException("NARG");
49 * Adds a file to the un-versioned space.
51 * @param __path The file to add.
52 * @param __target The target destination.
53 * @throws IOException On read/write errors.
54 * @throws NullPointerException On null arguments.
57 public void add(Path __path
, String __target
)
58 throws IOException
, NullPointerException
60 if (__path
== null || __target
== null)
61 throw new NullPointerException("NARG");
63 this.exec("uv", "add",
64 __path
.toAbsolutePath().toString(), "--as", __target
);
68 * Executes the fossil command.
70 * @param __args The arguments to use.
71 * @throws IOException On read/write errors.
72 * @throws NullPointerException On null arguments.
75 public void exec(String
... __args
)
76 throws IOException
, NullPointerException
79 throw new NullPointerException("NARG");
81 // Add in all arguments
82 List
<String
> args
= new ArrayList
<>();
83 args
.add(this.exe
.toAbsolutePath().toString());
84 args
.addAll(Arrays
.asList(__args
));
87 ProcessBuilder builder
= new ProcessBuilder(args
);
88 builder
.redirectOutput(ProcessBuilder
.Redirect
.INHERIT
);
89 builder
.redirectError(ProcessBuilder
.Redirect
.INHERIT
);
91 // Start and wait for it to complete
92 Process process
= builder
.start();
95 int exit
= process
.waitFor();
97 throw new IOException("Exited with " + exit
);
99 catch (InterruptedException __e
)
101 throw new IOException("Interrupted", __e
);
106 * Finds the Fossil command.
108 * @return The resultant command or {@code null} if there is none.
111 public static FossilCommand
instance()
115 if (System
.getProperty("os.name").toLowerCase().contains("windows"))
116 exeName
= "fossil.exe";
121 String paths
= System
.getenv("PATH");
123 for (String path
: paths
.split(Pattern
.quote(File
.pathSeparator
)))
125 Path maybe
= Paths
.get(path
, exeName
);
126 if (Files
.exists(maybe
) && Files
.isExecutable(maybe
))
127 return new FossilCommand(maybe
);