Skip to content

Commit

Permalink
feat: added swift (#3271)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Nov 28, 2024
1 parent 5a0508f commit 1cfa6dc
Show file tree
Hide file tree
Showing 21 changed files with 396 additions and 45 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ num_cpus = "1"
number_prefix = "0.4"
once_cell = "1"
openssl = { version = "0.10", optional = true }
os-release = "0.1"
path-absolutize = "3"
petgraph = "0.6"
rand = "0.8"
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {
cfg_aliases::cfg_aliases! {
asdf: { any(feature = "asdf", not(target_os = "windows")) },
macos: { target_os = "macos" },
linux: { target_os = "linux" },
vfox: { any(feature = "vfox", target_os = "windows") },
}
built::write_built_file().expect("Failed to acquire build-time information");
Expand Down
19 changes: 19 additions & 0 deletions docs/lang/swift.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Swift

Swift is supported for macos and linux.

## Usage

Use the latest stable version of swift:

```sh
mise use -g swift
swift --version
```

## Settings

<script setup>
import Settings from '/components/settings.vue';
</script>
<Settings child="swift" :level="3" />
2 changes: 1 addition & 1 deletion docs/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ You can also specify the full name for a tool using `mise use aqua:1password/cli
| sver | [aqua:mitoma/sver](https://github.com/mitoma/sver) [asdf:robzr/asdf-sver](https://github.com/robzr/asdf-sver) |
| svu | [aqua:caarlos0/svu](https://github.com/caarlos0/svu) [asdf:asdf-community/asdf-svu](https://github.com/asdf-community/asdf-svu) |
| swag | [aqua:swaggo/swag](https://github.com/swaggo/swag) [asdf:behoof4mind/asdf-swag](https://github.com/behoof4mind/asdf-swag) |
| swift | [asdf:fcrespo82/asdf-swift](https://github.com/fcrespo82/asdf-swift) |
| swift | [core:swift](https://mise.jdx.dev/lang/swift.html) |
| swift-package-list | [asdf:MacPaw/asdf-swift-package-list](https://github.com/MacPaw/asdf-swift-package-list) |
| swiftformat | [asdf:younke/asdf-swiftformat](https://github.com/younke/asdf-swiftformat) |
| swiftgen | [asdf:younke/asdf-swiftgen](https://github.com/younke/asdf-swiftgen) |
Expand Down
21 changes: 0 additions & 21 deletions e2e/backend/test_spm_slow

This file was deleted.

21 changes: 21 additions & 0 deletions e2e/plugins/core/test_swift_slow
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

export MISE_EXPERIMENTAL=1
export MISE_USE_VERSIONS_HOST=0

MISE_DEBUG=1 mise use swift
assert_contains "mise x -- swift --version" "Swift version 6.0.2"

assert "mise x spm:nicklockwood/SwiftFormat@0.53.10 -- swiftformat --version" "0.53.10"
assert "mise x spm:https://github.com/nicklockwood/SwiftFormat.git@0.53.10 -- swiftformat --version" "0.53.10"

# test package with resources (`templates list` command depends on resources being installed)
#assert "mise x spm:SwiftGen/SwiftGen@6.6.2 --verbose -- swiftgen templates list --only colors" "colors:
# - literals-swift4
# - literals-swift5
# - swift4
# - swift5
#---
#You can also specify custom templates by path, using \`templatePath\` instead of \`templateName\`.
#For more information, see the documentation on GitHub or use \`swiftgen template doc\`.
#"
1 change: 1 addition & 0 deletions mise.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ slsa-verifier-macos-aarch64 = "sha256:8740e66832fd48bbaa479acd5310986b876ff54546
version = "0.1.6"

[tools.wait-for-gh-rate-limit.checksums]
wait-for-gh-rate-limit-linux-x86_64 = "sha256:f02a40ac86bd1777151fe1ba3a7ee74c10174944e1b43a8bdd19af7021ad541f"
wait-for-gh-rate-limit-macos-aarch64 = "sha256:17a7c99021d8264071d70974bffd69741690da25a65518f515fd0c81ddc75c19"
3 changes: 2 additions & 1 deletion registry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,8 @@ sui.backends = ["asdf:placeholder-soft/asdf-sui"]
sver.backends = ["aqua:mitoma/sver", "asdf:robzr/asdf-sver"]
svu.backends = ["aqua:caarlos0/svu", "asdf:asdf-community/asdf-svu"]
swag.backends = ["aqua:swaggo/swag", "asdf:behoof4mind/asdf-swag"]
swift.backends = ["asdf:fcrespo82/asdf-swift"]
swift.backends = ["core:swift"]
swift.os = ["linux", "macos"]
swift-package-list.backends = ["asdf:MacPaw/asdf-swift-package-list"]
swiftformat.backends = ["asdf:younke/asdf-swiftformat"]
swiftgen.backends = ["asdf:younke/asdf-swiftgen"]
Expand Down
9 changes: 9 additions & 0 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,15 @@
}
}
},
"swift": {
"additionalProperties": false,
"properties": {
"gpg_verify": {
"description": "Use gpg to verify swift tool signatures.",
"type": "boolean"
}
}
},
"task_output": {
"description": "Change output style when executing tasks.",
"type": "string",
Expand Down
6 changes: 6 additions & 0 deletions settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,12 @@ env = "MISE_STATUS_MESSAGE_SHOW_TOOLS"
type = "Bool"
description = "Show configured env vars when entering a directory with a mise.toml file."

[swift.gpg_verify]
env = "MISE_SWIFT_GPG_VERIFY"
type = "Bool"
optional = true
description = "Use gpg to verify swift tool signatures."

[task_output]
env = "MISE_TASK_OUTPUT"
type = "String"
Expand Down
13 changes: 8 additions & 5 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use indexmap::IndexSet;
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use sha2::Sha256;

pub mod aqua;
pub mod asdf;
Expand Down Expand Up @@ -471,7 +470,9 @@ pub trait Backend: Debug + Send + Sync {
remove_all_with_warning(dir)
};
rmdir(&tv.install_path())?;
rmdir(&tv.download_path())?;
if !SETTINGS.always_keep_download {
rmdir(&tv.download_path())?;
}
rmdir(&tv.cache_path())?;
Ok(())
}
Expand Down Expand Up @@ -526,7 +527,9 @@ pub trait Backend: Debug + Send + Sync {

fn create_install_dirs(&self, tv: &ToolVersion) -> eyre::Result<()> {
let _ = remove_all_with_warning(tv.install_path());
let _ = remove_all_with_warning(tv.download_path());
if !SETTINGS.always_keep_download {
let _ = remove_all_with_warning(tv.download_path());
}
let _ = remove_all_with_warning(tv.cache_path());
let _ = file::remove_file(tv.install_path()); // removes if it is a symlink
file::create_dir_all(tv.install_path())?;
Expand All @@ -542,7 +545,7 @@ pub trait Backend: Debug + Send + Sync {
}
}
fn cleanup_install_dirs(&self, tv: &ToolVersion) {
if !SETTINGS.always_keep_download && !SETTINGS.always_keep_install {
if !SETTINGS.always_keep_download {
let _ = remove_all_with_warning(tv.download_path());
}
}
Expand Down Expand Up @@ -642,7 +645,7 @@ pub trait Backend: Debug + Send + Sync {
}
} else if SETTINGS.lockfile && SETTINGS.experimental {
ctx.pr.set_message(format!("generate checksum {filename}"));
let hash = hash::file_hash_prog::<Sha256>(file, Some(ctx.pr.as_ref()))?;
let hash = hash::file_hash_sha256(file, Some(ctx.pr.as_ref()))?;
tv.checksums.insert(filename, format!("sha256:{hash}"));
}
Ok(())
Expand Down
3 changes: 1 addition & 2 deletions src/backend/spm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ impl Backend for SPMBackend {
}

fn get_dependencies(&self) -> eyre::Result<Vec<&str>> {
// TODO: swift as dependencies (wait for swift core plugin: https://github.com/jdx/mise/pull/1708)
Ok(vec![])
Ok(vec!["swift"])
}

fn _list_remote_versions(&self) -> eyre::Result<Vec<String>> {
Expand Down
3 changes: 1 addition & 2 deletions src/backend/ubi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{file, github, hash};
use eyre::bail;
use itertools::Itertools;
use regex::Regex;
use sha2::Sha256;
use std::env;
use std::fmt::Debug;
use std::path::Path;
Expand Down Expand Up @@ -206,7 +205,7 @@ impl Backend for UbiBackend {
} else if SETTINGS.lockfile && SETTINGS.experimental {
ctx.pr
.set_message(format!("checksum generate {checksum_key}"));
let hash = hash::file_hash_prog::<Sha256>(file, Some(ctx.pr.as_ref()))?;
let hash = hash::file_hash_sha256(file, Some(ctx.pr.as_ref()))?;
tv.checksums.insert(checksum_key, format!("sha256:{hash}"));
}
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use crate::config::env_directive::EnvDirective;
use crate::config::{AliasMap, Settings};
use crate::errors::Error::UntrustedConfig;
use crate::file::display_path;
use crate::hash::{file_hash_sha256, hash_to_str};
use crate::hash::hash_to_str;
use crate::task::Task;
use crate::toolset::{ToolRequest, ToolRequestSet, ToolSource, ToolVersionList, Toolset};
use crate::ui::{prompt, style};
use crate::{backend, config, dirs, env, file};
use crate::{backend, config, dirs, env, file, hash};

pub mod idiomatic_version;
pub mod mise_toml;
Expand Down Expand Up @@ -348,7 +348,7 @@ pub fn trust(path: &Path) -> eyre::Result<()> {
file::make_symlink_or_file(path.canonicalize()?.as_path(), &hashed_path)?;
}
let trust_hash_path = hashed_path.with_extension("hash");
let hash = file_hash_sha256(path)?;
let hash = hash::file_hash_sha256(path, None)?;
file::write(trust_hash_path, hash)?;
Ok(())
}
Expand Down Expand Up @@ -409,7 +409,7 @@ fn trust_file_hash(path: &Path) -> eyre::Result<bool> {
return Ok(false);
}
let hash = file::read_to_string(&trust_hash_path)?;
let actual = file_hash_sha256(path)?;
let actual = hash::file_hash_sha256(path, None)?;
Ok(hash == actual)
}

Expand Down
15 changes: 15 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ pub fn open<P: AsRef<Path>>(path: P) -> Result<File> {
File::open(path).wrap_err_with(|| format!("failed open: {}", display_path(path)))
}

#[allow(unused)]
pub fn read<P: AsRef<Path>>(path: P) -> Result<Vec<u8>> {
let path = path.as_ref();
trace!("cat {}", display_path(path));
fs::read(path).wrap_err_with(|| format!("failed read: {}", display_path(path)))
}

pub fn size<P: AsRef<Path>>(path: P) -> Result<u64> {
let path = path.as_ref();
trace!("du -b {}", display_path(path));
path.metadata()
.map(|m| m.len())
.wrap_err_with(|| format!("failed size: {}", display_path(path)))
}

pub fn append<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> {
let path = path.as_ref();
trace!("append {}", display_path(path));
Expand Down
42 changes: 35 additions & 7 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ pub fn hash_sha256_to_str(s: &str) -> String {
format!("{:x}", hasher.finalize())
}

pub fn file_hash_sha256(path: &Path) -> Result<String> {
file_hash_prog::<Sha256>(path, None)
pub fn file_hash_sha256(path: &Path, pr: Option<&dyn SingleReport>) -> Result<String> {
let use_external_hasher = file::size(path).unwrap_or_default() > 50 * 1024 * 1024;
if use_external_hasher && file::which("sha256sum").is_some() {
let out = cmd!("sha256sum", path).read()?;
Ok(out.split_whitespace().next().unwrap().to_string())
} else {
file_hash_prog::<Sha256>(path, pr)
}
}

pub fn file_hash_prog<D>(path: &Path, pr: Option<&dyn SingleReport>) -> Result<String>
fn file_hash_prog<D>(path: &Path, pr: Option<&dyn SingleReport>) -> Result<String>
where
D: Digest + Write,
D::OutputSize: std::ops::Add,
Expand Down Expand Up @@ -63,11 +69,33 @@ pub fn ensure_checksum(
pr: Option<&dyn SingleReport>,
algo: &str,
) -> Result<()> {
let use_external_hasher = file::size(path).unwrap_or(u64::MAX) > 10 * 1024 * 1024;
let actual = match algo {
"sha512" => file_hash_prog::<Sha512>(path, pr)?,
"sha512" => {
if use_external_hasher && file::which("sha512sum").is_some() {
let out = cmd!("sha512sum", path).read()?;
out.split_whitespace().next().unwrap().to_string()
} else {
file_hash_prog::<Sha512>(path, pr)?
}
}
"sha256" => file_hash_prog::<Sha256>(path, pr)?,
"sha1" => file_hash_prog::<Sha1>(path, pr)?,
"md5" => file_hash_prog::<Md5>(path, pr)?,
"sha1" => {
if use_external_hasher && file::which("sha1sum").is_some() {
let out = cmd!("sha1sum", path).read()?;
out.split_whitespace().next().unwrap().to_string()
} else {
file_hash_prog::<Sha1>(path, pr)?
}
}
"md5" => {
if use_external_hasher && file::which("md5sum").is_some() {
let out = cmd!("md5sum", path).read()?;
out.split_whitespace().next().unwrap().to_string()
} else {
file_hash_prog::<Md5>(path, pr)?
}
}
_ => bail!("Unknown checksum algorithm: {}", algo),
};
let checksum = checksum.to_lowercase();
Expand Down Expand Up @@ -105,7 +133,7 @@ mod tests {
#[test]
fn test_hash_sha256() {
let path = Path::new(".test-tool-versions");
let hash = file_hash_sha256(path).unwrap();
let hash = file_hash_prog::<Sha256>(path, None).unwrap();
assert_snapshot!(hash);
}
}
2 changes: 1 addition & 1 deletion src/plugins/core/bun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Backend for BunPlugin {
&self,
ctx: &InstallContext,
mut tv: ToolVersion,
) -> eyre::Result<ToolVersion> {
) -> Result<ToolVersion> {
let tarball_path = self.download(&tv, ctx.pr.as_ref())?;
self.verify_checksum(ctx, &mut tv, &tarball_path)?;
self.install(ctx, &tv, &tarball_path)?;
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::plugins::core::java::JavaPlugin;
use crate::plugins::core::node::NodePlugin;
use crate::plugins::core::ruby::RubyPlugin;
use crate::plugins::core::rust::RustPlugin;
use crate::plugins::core::swift::SwiftPlugin;
#[cfg(unix)]
use crate::plugins::core::zig::ZigPlugin;
use crate::timeout::run_with_timeout;
Expand All @@ -35,6 +36,7 @@ mod python;
#[cfg_attr(windows, path = "ruby_windows.rs")]
mod ruby;
mod rust;
mod swift;
#[cfg(unix)]
mod zig;

Expand All @@ -50,6 +52,7 @@ pub static CORE_PLUGINS: Lazy<BackendMap> = Lazy::new(|| {
Arc::new(PythonPlugin::new()),
Arc::new(RubyPlugin::new()),
Arc::new(RustPlugin::new()),
Arc::new(SwiftPlugin::new()),
Arc::new(ZigPlugin::new()),
];
#[cfg(windows)]
Expand All @@ -63,6 +66,7 @@ pub static CORE_PLUGINS: Lazy<BackendMap> = Lazy::new(|| {
Arc::new(PythonPlugin::new()),
Arc::new(RubyPlugin::new()),
Arc::new(RustPlugin::new()),
Arc::new(SwiftPlugin::new()),
// Arc::new(ZigPlugin::new()),
];
plugins
Expand Down
Loading

0 comments on commit 1cfa6dc

Please sign in to comment.