From 8059cfb5d91b130dc8cf8d1fa334973a6bbcbe75 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Wed, 1 Jul 2020 17:01:38 +0200 Subject: [PATCH] Add support for legacy config directory on macOS See discussion in https://github.com/dirs-dev/directories-jvm/pull/34 --- doc/docs/other-credentials.md | 2 +- .../scala/coursier/cache/CacheDefaults.scala | 12 +++--- .../scala/coursier/cli/install/Install.scala | 2 +- .../cli/install/SharedChannelParams.scala | 10 +++-- .../main/scala/coursier/PlatformResolve.scala | 2 +- .../java/coursier/paths/CoursierPaths.java | 39 ++++++++++++++----- .../src/main/java/coursier/paths/Mirror.java | 21 +++++++--- 7 files changed, 59 insertions(+), 29 deletions(-) diff --git a/doc/docs/other-credentials.md b/doc/docs/other-credentials.md index a7309cc00f..cd5a536525 100644 --- a/doc/docs/other-credentials.md +++ b/doc/docs/other-credentials.md @@ -48,7 +48,7 @@ Only `--credentials` and `--credential-file` are considered then. Credentials can be passed via property files too. By default, `~/.config/coursier/credentials.properties` is read -(`~/Library/Preferences/Coursier/credentials.properties` on OS X). +(`~/Library/Application Support/Coursier/credentials.properties` on OS X). ### Format diff --git a/modules/cache/jvm/src/main/scala/coursier/cache/CacheDefaults.scala b/modules/cache/jvm/src/main/scala/coursier/cache/CacheDefaults.scala index 124c103c34..81535d7587 100644 --- a/modules/cache/jvm/src/main/scala/coursier/cache/CacheDefaults.scala +++ b/modules/cache/jvm/src/main/scala/coursier/cache/CacheDefaults.scala @@ -88,20 +88,20 @@ object CacheDefaults { def credentials: Seq[Credentials] = if (credentialPropOpt.isEmpty) { // Warn if those files have group and others read permissions? - val configDir = coursier.paths.CoursierPaths.configDirectory() - val mainCredentialsFile = new File(configDir, "credentials.properties") + val configDirs = coursier.paths.CoursierPaths.configDirectories().toSeq + val mainCredentialsFiles = configDirs.map(configDir => new File(configDir, "credentials.properties")) val otherFiles = { // delay listing files until credentials are really needed? - val dir = new File(configDir, "credentials") - val files = dir.listFiles(new FilenameFilter { + val dirs = configDirs.map(configDir => new File(configDir, "credentials")) + val files = dirs.flatMap(_.listFiles(new FilenameFilter { def accept(dir: File, name: String): Boolean = !name.startsWith(".") && name.endsWith(".properties") - }) + })) Option(files).toSeq.flatten.map { f => FileCredentials(f.getAbsolutePath, optional = true) // non optional? } } - FileCredentials(mainCredentialsFile.getAbsolutePath, optional = true) +: otherFiles + mainCredentialsFiles.map(f => FileCredentials(f.getAbsolutePath, optional = true)) ++ otherFiles } else credentialPropOpt .filter(isPropFile) diff --git a/modules/cli/src/main/scala/coursier/cli/install/Install.scala b/modules/cli/src/main/scala/coursier/cli/install/Install.scala index f22e6955f3..93ecc25466 100644 --- a/modules/cli/src/main/scala/coursier/cli/install/Install.scala +++ b/modules/cli/src/main/scala/coursier/cli/install/Install.scala @@ -45,7 +45,7 @@ object Install extends CaseApp[InstallOptions] { // TODO Move to install module - val configDir = coursier.paths.CoursierPaths.configDirectory() + val configDir = coursier.paths.CoursierPaths.defaultConfigDirectory() val channelDir = new File(configDir, "channels") // FIXME May not be fine with concurrency (two process doing this in parallel) diff --git a/modules/cli/src/main/scala/coursier/cli/install/SharedChannelParams.scala b/modules/cli/src/main/scala/coursier/cli/install/SharedChannelParams.scala index 2af85f3cfe..a96cd148ff 100644 --- a/modules/cli/src/main/scala/coursier/cli/install/SharedChannelParams.scala +++ b/modules/cli/src/main/scala/coursier/cli/install/SharedChannelParams.scala @@ -34,10 +34,12 @@ object SharedChannelParams { val fileChannelsV = if (options.fileChannels) { - val configDir = coursier.paths.CoursierPaths.configDirectory() - val channelDir = new File(configDir, "channels") - val files = Option(channelDir.listFiles()) - .getOrElse(Array.empty[File]) + val configDirs = coursier.paths.CoursierPaths.configDirectories().toSeq + val files = configDirs + .flatMap { configDir => + val channelDir = new File(configDir, "channels") + Option(channelDir.listFiles()).getOrElse(Array.empty[File]) + } .filter(f => !f.getName.startsWith(".")) val rawChannels = files.toList.flatMap { f => val b = Files.readAllBytes(f.toPath) diff --git a/modules/coursier/jvm/src/main/scala/coursier/PlatformResolve.scala b/modules/coursier/jvm/src/main/scala/coursier/PlatformResolve.scala index 203553bf9a..b54a5d3a3e 100644 --- a/modules/coursier/jvm/src/main/scala/coursier/PlatformResolve.scala +++ b/modules/coursier/jvm/src/main/scala/coursier/PlatformResolve.scala @@ -8,7 +8,7 @@ import coursier.parse.RepositoryParser abstract class PlatformResolve { def defaultMirrorConfFiles: Seq[MirrorConfFile] = { - val files = Seq(coursier.paths.Mirror.defaultConfigFile()) ++ + val files = coursier.paths.Mirror.defaultConfigFiles().toSeq ++ Option(coursier.paths.Mirror.extraConfigFile()).toSeq files.map { f => // Warn if f has group and others read permissions? diff --git a/modules/paths/src/main/java/coursier/paths/CoursierPaths.java b/modules/paths/src/main/java/coursier/paths/CoursierPaths.java index 42a835752d..ec1d95b456 100644 --- a/modules/paths/src/main/java/coursier/paths/CoursierPaths.java +++ b/modules/paths/src/main/java/coursier/paths/CoursierPaths.java @@ -26,7 +26,7 @@ private CoursierPaths() { private static volatile File jvmCacheDirectory0 = null; private static final Object configDirectoryLock = new Object(); - private static volatile File configDirectory0 = null; + private static volatile File[] configDirectories0 = null; private static final Object dataLocalDirectoryLock = new Object(); private static volatile File dataLocalDirectory0 = null; @@ -94,28 +94,47 @@ private static ProjectDirectories coursierDirectories() throws IOException { return coursierDirectories0; } - private static String computeConfigDirectory() throws IOException { + private static File[] computeConfigDirectories() throws IOException { String path = System.getenv("COURSIER_CONFIG_DIR"); if (path == null) path = System.getProperty("coursier.config-dir"); if (path != null) - return path; - - return coursierDirectories().configDir; + return new File[] { new File(path).getAbsoluteFile() }; + + String configDir = coursierDirectories().configDir; + String preferenceDir = coursierDirectories().preferenceDir; + if (configDir.equals(preferenceDir)) + return new File[] { + new File(configDir).getAbsoluteFile(), + }; + else + return new File[] { + new File(configDir).getAbsoluteFile(), + new File(preferenceDir).getAbsoluteFile() + }; } - public static File configDirectory() throws IOException { + public static File[] configDirectories() throws IOException { - if (configDirectory0 == null) + if (configDirectories0 == null) synchronized (configDirectoryLock) { - if (configDirectory0 == null) { - configDirectory0 = new File(computeConfigDirectory()).getAbsoluteFile(); + if (configDirectories0 == null) { + configDirectories0 = computeConfigDirectories(); } } - return configDirectory0; + return configDirectories0.clone(); + } + + @Deprecated + public static File configDirectory() throws IOException { + return configDirectories()[0]; + } + + public static File defaultConfigDirectory() throws IOException { + return configDirectories()[0]; } private static String computeDataLocalDirectory() throws IOException { diff --git a/modules/paths/src/main/java/coursier/paths/Mirror.java b/modules/paths/src/main/java/coursier/paths/Mirror.java index 0431e43639..29c5f66029 100644 --- a/modules/paths/src/main/java/coursier/paths/Mirror.java +++ b/modules/paths/src/main/java/coursier/paths/Mirror.java @@ -21,21 +21,30 @@ public static Mirror of(List from, String to, String type) { return new Mirror(from, to, type); } - public static File defaultConfigFile() throws IOException { + public static File[] defaultConfigFiles() throws IOException { String fromEnv = System.getenv("COURSIER_MIRRORS"); if (fromEnv != null && !fromEnv.isEmpty()) { - return new File(fromEnv); + return new File[] { new File(fromEnv) }; } String fromProps = System.getProperty("coursier.mirrors"); if (fromProps != null && !fromProps.isEmpty()) { - return new File(fromProps); + return new File[] { new File(fromProps) }; } - File configDir = coursier.paths.CoursierPaths.configDirectory(); - File propFile = new File(configDir, "mirror.properties"); - return propFile; + File[] configDirs = coursier.paths.CoursierPaths.configDirectories(); + ArrayList configFiles = new ArrayList<>(); + for (File configDir : configDirs) { + File propFile = new File(configDir, "mirror.properties"); + configFiles.add(propFile); + } + return configFiles.toArray(new File[configFiles.size()]); + } + + @Deprecated + public static File defaultConfigFile() throws IOException { + return defaultConfigFiles()[0]; } public static File extraConfigFile() throws IOException {