Add missing files.
[SquirrelJME.git] / tools / cicd-release-bundle / src / main / java / cc / squirreljme / cicd / FossilCommand.java
blobbcba8279b8dd9fd985132ddb180f5a6a703f93da
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;
12 import java.io.File;
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;
22 /**
23 * Access to the Fossil SCM command line interface.
25 * @since 2024/10/05
27 public class FossilCommand
29 /** The executable path. */
30 protected final Path exe;
32 /**
33 * Initializes the command executor.
35 * @param __exe The executable to use.
36 * @throws NullPointerException On null arguments.
37 * @since 2024/10/05
39 public FossilCommand(Path __exe)
40 throws NullPointerException
42 if (__exe == null)
43 throw new NullPointerException("NARG");
45 this.exe = __exe;
48 /**
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.
55 * @since 2024/10/05
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);
67 /**
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.
73 * @since 2024/10/05
75 public void exec(String... __args)
76 throws IOException, NullPointerException
78 if (__args == null)
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));
86 // Setup process
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();
93 try
95 int exit = process.waitFor();
96 if (exit != 0)
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.
109 * @since 2024/10/05
111 public static FossilCommand instance()
113 // Windows or not?
114 String exeName;
115 if (System.getProperty("os.name").toLowerCase().contains("windows"))
116 exeName = "fossil.exe";
117 else
118 exeName = "fossil";
120 // Use system PATH
121 String paths = System.getenv("PATH");
122 if (paths != null)
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);
130 // Not found
131 return null;