Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 58 additions & 41 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,47 +1,64 @@
lazy val root = (project in file("."))
lazy val sharedSettings = Def.settings(
organization := "dev.dirs",
managedScalaInstance := false,
crossPaths := false,
version := "20",
homepage := Some(url("https://github.com/dirs-dev/directories-jvm")),
licenses := Seq("Mozilla Public License 2.0" -> url("https://opensource.org/licenses/MPL-2.0")),
fork := true,
// The javaHome setting can be removed if building against the latest installed version of Java is acceptable.
// Running the tests requires removing the setting.
// It can also be changed to point to a different Java version.
// javaHome := Some(file("/home/soc/apps/zulu6.19.0.1-jdk6.0.103-linux_x64/")),
/*
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value) Some("snapshots" at nexus + "content/repositories/snapshots")
else Some("releases" at nexus + "service/local/staging/deploy/maven2")
},
*/
pomIncludeRepository := { _ => false },
pomExtra :=
<scm>
<url>[email protected]:dirs-dev/directories-jvm.git</url>
<connection>scm:git:[email protected]:dirs-dev/directories-jvm.git</connection>
</scm>
<developers>
<developer>
<id>soc</id>
<name>Simon Ochsenreither</name>
<url>https://github.com/soc</url>
<roles>
<role>Project Lead</role>
</roles>
</developer>
</developers>
)

def automaticModuleName(name: String) = Def.settings(
packageOptions in (Compile, packageBin) += {
import java.util.jar.{Attributes, Manifest}
val manifest = new Manifest
manifest.getMainAttributes.put(new Attributes.Name("Automatic-Module-Name"), name)
Package.JarManifest(manifest)
}
)

lazy val core = project
.settings(
sharedSettings,
name := "directories",
organization := "dev.dirs",
managedScalaInstance := false,
crossPaths := false,
version := "20",
homepage := Some(url("https://github.com/dirs-dev/directories-jvm")),
licenses := Seq("Mozilla Public License 2.0" -> url("https://opensource.org/licenses/MPL-2.0")),
fork := true,
// The javaHome setting can be removed if building against the latest installed version of Java is acceptable.
// Running the tests requires removing the setting.
// It can also be changed to point to a different Java version.
// javaHome := Some(file("/home/soc/apps/zulu6.19.0.1-jdk6.0.103-linux_x64/")),
libraryDependencies += "junit" % "junit" % "4.13" % Test,
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test,
testOptions in Test := Seq(Tests.Argument(TestFrameworks.JUnit, "-a")),
/*
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value) Some("snapshots" at nexus + "content/repositories/snapshots")
else Some("releases" at nexus + "service/local/staging/deploy/maven2")
},
*/
packageOptions in (Compile, packageBin) += {
import java.util.jar.{Attributes, Manifest}
val manifest = new Manifest
manifest.getMainAttributes.put(new Attributes.Name("Automatic-Module-Name"), "dev.dirs")
Package.JarManifest(manifest)
},
pomIncludeRepository := { _ => false },
pomExtra :=
<scm>
<url>[email protected]:dirs-dev/directories-jvm.git</url>
<connection>scm:git:[email protected]:dirs-dev/directories-jvm.git</connection>
</scm>
<developers>
<developer>
<id>soc</id>
<name>Simon Ochsenreither</name>
<url>https://github.com/soc</url>
<roles>
<role>Project Lead</role>
</roles>
</developer>
</developers>
automaticModuleName("dev.dirs")
)

lazy val cli = project
.dependsOn(core)
.settings(
sharedSettings,
name := "directories-cli",
libraryDependencies += "info.picocli" % "picocli" % "4.4.0",
automaticModuleName("dev.dirs.cli")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

)
71 changes: 71 additions & 0 deletions cli/src/main/java/dev/dirs/cli/DirectoriesCli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.dirs.cli;

import dev.dirs.ProjectDirectories;
import picocli.CommandLine;
import picocli.CommandLine.*;

@Command(name = "directories", mixinStandardHelpOptions = true, version = "20")
public class DirectoriesCli implements Runnable {
@Option(names = {"--cache"}, description = "Prints cache directory")
boolean cache;
@Option(names = {"--config"}, description = "Prints config directory")
boolean config;
@Option(names = {"--data"}, description = "Prints data directory")
boolean data;
@Option(names = {"--data-local"}, description = "Prints local data directory")
boolean dataLocal;
@Option(names = {"--preferences"}, description = "Prints preferences directory")
boolean preferences;
@Option(names = {"--runtime"}, description = "Prints runtime directory")
boolean runtime;

@Parameters(index = "0")
String project;

@Override
public void run() {
int dirCount = 0;
if (cache) dirCount += 1;
if (config) dirCount += 1;
if (data) dirCount += 1;
if (dataLocal) dirCount += 1;
if (preferences) dirCount += 1;
if (runtime) dirCount += 1;

boolean all = dirCount == 0;

ProjectDirectories projDirs = ProjectDirectories.from(null, null, project);

if (dirCount == 1) {
if (cache)
System.out.println(projDirs.cacheDir);
if (config)
System.out.println(projDirs.configDir);
if (data)
System.out.println(projDirs.dataDir);
if (dataLocal)
System.out.println(projDirs.dataLocalDir);
if (preferences)
System.out.println(projDirs.preferenceDir);
if (runtime)
System.out.println(projDirs.runtimeDir);
} else {
if (all || cache)
System.out.println("cache: " + projDirs.cacheDir);
if (all || config)
System.out.println("config: " + projDirs.configDir);
if (all || data)
System.out.println("data: " + projDirs.dataDir);
if (all || dataLocal)
System.out.println("local data: " + projDirs.dataLocalDir);
if (all || preferences)
System.out.println("preference: " + projDirs.preferenceDir);
if (all || runtime)
System.out.println("runtime: " + projDirs.runtimeDir);
}
}

public static void main(String... args) {
System.exit(new CommandLine(new DirectoriesCli()).execute(args));
}
}
File renamed without changes.