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

Stop using package.authors #164

Merged
merged 2 commits into from
Aug 15, 2021
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
}
```

- No longer uses [`package.authors`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-authors-field) to skip Copyright and License Notices.

- Now uses rust-analyzer `2021-07-12` for Rust 1.47 and `2021-08-09` for Rust 1.48+.

### Fixed
Expand Down
34 changes: 7 additions & 27 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "cargo-equip"
version = "0.17.0"
authors = ["Ryo Yamashita <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
description = "A Cargo subcommand to bundle your code into one `.rs` file for competitive programming."
Expand All @@ -10,8 +9,7 @@ keywords = ["competitive", "cli", "windows"]
categories = ["command-line-utilities", "development-tools::cargo-plugins"]

[dependencies]
anyhow = "1.0.42"
arrayvec = { version = "0.7.1", features = ["serde"] }
anyhow = "1.0.43"
atty = "0.2.14"
camino = { version = "1.0.5", features = ["serde1"] }
cargo_metadata = "0.14.0"
Expand Down Expand Up @@ -42,7 +40,6 @@ structopt = "0.3.22"
syn = { version = "1.0.74", features = ["extra-traits", "full", "parsing", "visit"] }
tempfile = "3.2.0"
termcolor = "1.1.2"
toml = "0.5.8"
toml_edit = "0.2.1"
which = "4.2.2"
xshell = "0.1.14"
Expand Down
29 changes: 8 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
shell::Shell,
workspace::{MetadataExt as _, PackageExt as _, PackageIdExt as _, TargetExt as _},
};
use anyhow::{ensure, Context as _};
use anyhow::Context as _;
use cargo_metadata as cm;
use indoc::indoc;
use itertools::{iproduct, Itertools as _};
Expand All @@ -43,7 +43,7 @@ use structopt::{clap::AppSettings, StructOpt};

#[derive(StructOpt, Debug)]
#[structopt(
author,
author("Ryo Yamashita <[email protected]>"),
about("Please run as `cargo equip`, not `cargo-equip`."),
bin_name("cargo"),
global_settings(&[AppSettings::DeriveDisplayOrder, AppSettings::UnifiedHelpMessage])
Expand All @@ -56,7 +56,7 @@ pub enum Opt {

Use -h for short descriptions and --help for more detials.
"#}),
author,
author("Ryo Yamashita <[email protected]>"),
usage(
r#"cargo equip [OPTIONS]
cargo equip [OPTIONS] --src <PATH>
Expand Down Expand Up @@ -841,18 +841,11 @@ fn bundle(
>>()?;

if !libs.is_empty() {
let authors = if root_crate.package().authors.is_empty() {
workspace::attempt_get_author(&metadata.workspace_root)?
.into_iter()
.collect()
} else {
root_crate.package().authors.clone()
};

ensure!(
!authors.is_empty(),
"cannot know who you are. see https://github.com/qryxip/cargo-equip/issues/120",
);
if !root_crate.package().authors.is_empty() {
shell.warn(
"`package.authors` are no longer used to skip Copyright and License Notices",
)?;
}

code = rust::insert_prelude_for_main_crate(&code, cargo_equip_mod_name)?;

Expand Down Expand Up @@ -946,12 +939,6 @@ fn bundle(
.iter()
.filter(|(_, (p, _, _, _, _))| p.has_lib())
.map(|(_, (p, _, _, _, _))| p)
.filter(|lib_package| {
lib_package.id == root_crate.package().id
|| !authors
.iter()
.all(|author| lib_package.authors.contains(author))
})
.flat_map(|lib_package| {
if let Err(err) = shell.status(
"Reading",
Expand Down
35 changes: 1 addition & 34 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ mod license;

use crate::{shell::Shell, toolchain};
use anyhow::{bail, Context as _};
use arrayvec::ArrayVec;
use camino::{Utf8Path, Utf8PathBuf};
use cargo_metadata as cm;
use if_chain::if_chain;
use indoc::indoc;
use itertools::Itertools as _;
use krates::PkgSpec;
use rand::Rng as _;
use serde::Deserialize;
use std::{
collections::{BTreeMap, HashMap, HashSet},
env,
Expand Down Expand Up @@ -82,37 +80,6 @@ pub(crate) fn list_out_dirs<'cm>(
.collect()
}

pub(crate) fn attempt_get_author(workspace_root: &Utf8Path) -> anyhow::Result<Option<String>> {
#[derive(Deserialize)]
struct Manifest {
package: ManifestPackage,
}

#[derive(Deserialize)]
struct ManifestPackage {
#[serde(default)]
authors: ArrayVec<String, 1>,
}

let tempdir = tempfile::Builder::new()
.prefix("cargo-equip-get-author-")
.tempdir()?;

crate::process::process(crate::process::cargo_exe()?)
.args(&["new", "-q", "--vcs", "none"])
.arg(tempdir.path().join("a"))
.cwd(workspace_root)
.exec()?;

let manifest = xshell::read_file(tempdir.path().join("a").join("Cargo.toml"))?;
let author = toml::from_str::<Manifest>(&manifest)?
.package
.authors
.get(0)
.cloned();
Ok(author)
}

pub(crate) fn cargo_check_using_current_lockfile_and_cache(
metadata: &cm::Metadata,
package: &cm::Package,
Expand Down Expand Up @@ -692,7 +659,7 @@ impl PackageExt for cm::Package {
}

fn read_license_text(&self, cache_dir: &Path) -> anyhow::Result<Option<String>> {
license::license_file(self, cache_dir)
license::read_non_unlicense_license_file(self, cache_dir)
}
}

Expand Down
38 changes: 32 additions & 6 deletions src/workspace/license.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
use crate::workspace::{PackageExt as _, SourceExt as _};
use anyhow::{anyhow, bail, Context as _};
use anyhow::{anyhow, Context as _};
use cargo_metadata as cm;
use serde::Deserialize;
use std::path::Path;

pub(super) fn license_file(
pub(super) fn read_non_unlicense_license_file(
package: &cm::Package,
cache_dir: &Path,
) -> anyhow::Result<Option<String>> {
read(package, cache_dir).map_err(|causes| {
let err = anyhow!(
"could not read the license file of `{}`.\n\
note: cargo-equip no longer reads `package.authors` to skip Copyright and License \
Notices",
package.id
);
let mut causes = causes.into_iter();
if let Some(cause) = causes.next() {
causes
.fold(anyhow!("{}", cause), |e, s| e.context(s))
.context(err)
} else {
err
}
})
}

fn read(package: &cm::Package, cache_dir: &Path) -> Result<Option<String>, Vec<String>> {
let license = package
.license
.as_deref()
.with_context(|| format!("`{}`: missing `license`", package.id))?;
.ok_or_else(|| vec![format!("`{}`: missing `license`", package.id)])?;

let license = normalize(license);

let license = spdx::Expression::parse(license).map_err(|e| {
anyhow!("{}", e).context(format!("`{}`: could not parse `license`", package.id))
let license = spdx::Expression::parse(license).map_err(|err| {
vec![
err.to_string(),
format!("`{}`: could not parse `license`", package.id),
]
})?;

let is = |name| license.evaluate(|r| r.license.id() == spdx::license_id(name));
Expand All @@ -30,7 +52,10 @@ pub(super) fn license_file(
} else if is("Apache-2.0") {
&["LICENSE-APACHE", "LICENSE"]
} else {
bail!("`{}`: unsupported license: `{}`", package.id, license);
return Err(vec![format!(
"`{}`: unsupported license: `{}`",
package.id, license,
)]);
};

find(package.manifest_dir().as_ref(), file_names)
Expand Down Expand Up @@ -87,6 +112,7 @@ pub(super) fn license_file(
}
})
.map(Some)
.map_err(|e| vec![e.to_string()])
}

fn normalize(license: &str) -> &str {
Expand Down
1 change: 0 additions & 1 deletion tests/solutions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "solutions"
version = "0.0.0"
authors = ["Ryo Yamashita <[email protected]>"]
edition = "2018"
publish = false

Expand Down