Skip to content

Commit 886382a

Browse files
Merge pull request #9 from gitarcode/nn/merge-upstream
Merging upstream changes
2 parents 3ea1789 + f3f8abb commit 886382a

23 files changed

+302
-142
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Version 1.13.2
2+
- Fix binary name in --help --version so typeshare is the name and not typeshare-cli: [#214](https://github.com/1Password/typeshare/pull/214)
3+
4+
# Version 1.13.1
5+
- Fix duplicate root added to walker: [#209](https://github.com/1Password/typeshare/pull/209)
6+
- Only assert if go package is present if generating go types: [#211](https://github.com/1Password/typeshare/pull/211)
7+
- Update shell completions for new generate function: [#212](https://github.com/1Password/typeshare/pull/212)
8+
9+
# Version 1.13.0
10+
- Update how logging is initialized: [#206](https://github.com/1Password/typeshare/pull/206)
11+
- Don't recreate `Codable.swift` when the contents have not changed [#205](https://github.com/1Password/typeshare/pull/205)
12+
- Fix target_os parsing when no --target-os is provided [#204](https://github.com/1Password/typeshare/pull/204)
13+
114
# Version 1.12.0
215

316
- Optional slices in Go no longer trigger a pointer redirection.

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "typeshare-cli"
3-
version = "1.12.1"
3+
version = "1.13.2"
44
edition = "2021"
55
description = "Command Line Tool for generating language files with typeshare"
66
license = "MIT OR Apache-2.0"
@@ -16,17 +16,17 @@ go = []
1616

1717
[dependencies]
1818
clap = { version = "4.5", features = [
19-
"cargo",
20-
"derive",
21-
"unicode",
22-
"wrap_help",
19+
"cargo",
20+
"derive",
21+
"unicode",
22+
"wrap_help",
2323
] }
2424
ignore = "0.4"
2525
once_cell = "1"
2626
rayon = "1.10"
2727
serde = { version = "1", features = ["derive"] }
2828
toml = "0.8"
29-
typeshare-core = { path = "../core", version = "=1.12.1" }
29+
typeshare-core = { path = "../core", version = "=1.13.2" }
3030
log.workspace = true
3131
flexi_logger.workspace = true
3232
anyhow = "1"

cli/src/args.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub enum AvailableLanguage {
1616
#[command(
1717
version,
1818
args_conflicts_with_subcommands = true,
19-
subcommand_negates_reqs = true
19+
subcommand_negates_reqs = true,
20+
name = "typeshare"
2021
)]
2122
pub struct Args {
2223
#[command(subcommand)]

cli/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct SwiftParams {
3434
pub prefix: String,
3535
pub default_decorators: Vec<String>,
3636
pub default_generic_constraints: Vec<String>,
37-
/// The contraints to apply to `CodableVoid`.
37+
/// The constraints to apply to `CodableVoid`.
3838
pub codablevoid_constraints: Vec<String>,
3939
pub type_mappings: HashMap<String, String>,
4040
}
@@ -55,7 +55,7 @@ pub struct GoParams {
5555
pub type_mappings: HashMap<String, String>,
5656
}
5757

58-
/// The paramters that are used to configure the behaviour of typeshare
58+
/// The parameters that are used to configure the behaviour of typeshare
5959
/// from the configuration file `typeshare.toml`
6060
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
6161
#[serde(default)]

cli/src/main.rs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ use std::{
1515

1616
use anyhow::{anyhow, Context};
1717
use clap::{CommandFactory, Parser};
18-
use clap_complete::Generator;
18+
use clap_complete::aot::generate;
19+
use flexi_logger::AdaptiveFormat;
1920
use ignore::{overrides::OverrideBuilder, types::TypesBuilder, WalkBuilder};
20-
use log::error;
21+
use log::{error, info};
2122
use rayon::iter::ParallelBridge;
2223
#[cfg(feature = "go")]
2324
use typeshare_core::language::Go;
2425
use typeshare_core::{
26+
context::ParseContext,
2527
language::{
2628
CrateName, GenericConstraints, Kotlin, Language, Scala, SupportedLanguage, Swift,
2729
TypeScript,
@@ -37,17 +39,21 @@ use crate::{
3739
};
3840

3941
fn main() -> anyhow::Result<()> {
40-
flexi_logger::Logger::try_with_env()
41-
.unwrap()
42-
.start()
43-
.unwrap();
42+
flexi_logger::Logger::try_with_env_or_str("info")?
43+
.adaptive_format_for_stderr(AdaptiveFormat::Detailed)
44+
.adaptive_format_for_stdout(AdaptiveFormat::Detailed)
45+
.start()?;
4446

4547
let options = Args::parse();
4648

49+
info!("typeshare started generating types");
50+
4751
if let Some(options) = options.subcommand {
4852
match options {
4953
Command::Completions { shell } => {
50-
shell.generate(&Args::command(), &mut io::stdout().lock())
54+
let mut cmd = Args::command();
55+
let bin_name = cmd.get_name().to_string();
56+
generate(shell, &mut cmd, bin_name, &mut io::stdout());
5157
}
5258
}
5359

@@ -69,6 +75,8 @@ fn main() -> anyhow::Result<()> {
6975

7076
let directories = options.directories.as_slice();
7177

78+
info!("Using directories: {directories:?}");
79+
7280
let language_type = match options.language {
7381
None => panic!("no language specified; `clap` should have guaranteed its presence"),
7482
Some(language) => match language {
@@ -107,7 +115,7 @@ fn main() -> anyhow::Result<()> {
107115
.overrides(overrides)
108116
.follow_links(options.follow_links);
109117

110-
for root in directories {
118+
for root in directories.iter().skip(1) {
111119
walker_builder.add(root);
112120
}
113121

@@ -124,9 +132,13 @@ fn main() -> anyhow::Result<()> {
124132

125133
let multi_file = matches!(destination, Output::Folder(_));
126134
let target_os = config.target_os.clone();
127-
128135
let mut lang = language(language_type, config, multi_file);
129-
let ignored_types = lang.ignored_reference_types();
136+
137+
let parse_context = ParseContext {
138+
ignored_types: lang.ignored_reference_types(),
139+
multi_file,
140+
target_os,
141+
};
130142

131143
// The walker ignores directories that are git-ignored. If you need
132144
// a git-ignored directory to be processed, add the specific directory to
@@ -138,9 +150,7 @@ fn main() -> anyhow::Result<()> {
138150
// https://docs.rs/ignore/latest/ignore/struct.WalkParallel.html
139151
let crate_parsed_data = parse_input(
140152
parser_inputs(walker_builder, language_type, multi_file).par_bridge(),
141-
&ignored_types,
142-
multi_file,
143-
&target_os,
153+
&parse_context,
144154
)?;
145155

146156
// Collect all the types into a map of the file name they
@@ -153,13 +163,17 @@ fn main() -> anyhow::Result<()> {
153163
};
154164

155165
check_parse_errors(&crate_parsed_data)?;
166+
167+
info!("typeshare started writing generated types");
168+
156169
write_generated(
157170
destination,
158171
lang.as_mut(),
159172
crate_parsed_data,
160173
import_candidates,
161174
)?;
162175

176+
info!("typeshare finished generating types");
163177
Ok(())
164178
}
165179

@@ -244,7 +258,13 @@ fn override_configuration(mut config: Config, options: &Args) -> anyhow::Result<
244258
if let Some(go_package) = options.go_package.as_ref() {
245259
config.go.package = go_package.to_string();
246260
}
247-
assert_go_package_present(&config)?;
261+
262+
if matches!(options.language, Some(args::AvailableLanguage::Go)) {
263+
anyhow::ensure!(
264+
!config.go.package.is_empty(),
265+
"Please provide a package name in the typeshare.toml or using --go-package <package name>"
266+
);
267+
}
248268
}
249269

250270
config.target_os = options.target_os.as_deref().unwrap_or_default().to_vec();
@@ -262,25 +282,16 @@ fn check_parse_errors(parsed_crates: &BTreeMap<CrateName, ParsedData>) -> anyhow
262282
errors_encountered = true;
263283
for error in &data.errors {
264284
error!(
265-
"Parsing error: \"{}\" in crate \"{}\" for file \"{}\"",
266-
error.error, error.crate_name, error.file_name
285+
"Parsing error: \"{}\" in file \"{}\"",
286+
error.error, error.file_name
267287
);
268288
}
269289
}
270290

271291
if errors_encountered {
292+
error!("Errors encountered during parsing.");
272293
Err(anyhow!("Errors encountered during parsing."))
273294
} else {
274295
Ok(())
275296
}
276297
}
277-
278-
#[cfg(feature = "go")]
279-
fn assert_go_package_present(config: &Config) -> anyhow::Result<()> {
280-
if config.go.package.is_empty() {
281-
return Err(anyhow!(
282-
"Please provide a package name in the typeshare.toml or using --go-package <package name>"
283-
));
284-
}
285-
Ok(())
286-
}

cli/src/parse.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77
path::PathBuf,
88
};
99
use typeshare_core::{
10+
context::{ParseContext, ParseFileContext},
1011
language::{CrateName, CrateTypes, SupportedLanguage, SINGLE_FILE_CRATE_NAME},
1112
parser::ParsedData,
1213
RenameExt,
@@ -89,9 +90,7 @@ pub fn all_types(file_mappings: &BTreeMap<CrateName, ParsedData>) -> CrateTypes
8990
/// Collect all the parsed sources into a mapping of crate name to parsed data.
9091
pub fn parse_input(
9192
inputs: impl ParallelIterator<Item = ParserInput>,
92-
ignored_types: &[&str],
93-
multi_file: bool,
94-
target_os: &[String],
93+
parse_context: &ParseContext,
9594
) -> anyhow::Result<BTreeMap<CrateName, ParsedData>> {
9695
inputs
9796
.into_par_iter()
@@ -103,17 +102,17 @@ pub fn parse_input(
103102
file_name,
104103
crate_name,
105104
}| {
106-
let parsed_result = typeshare_core::parser::parse(
107-
&std::fs::read_to_string(&file_path)
105+
let parse_file_context = ParseFileContext {
106+
source_code: std::fs::read_to_string(&file_path)
108107
.with_context(|| format!("Failed to read input: {file_name}"))?,
109-
crate_name.clone(),
110-
file_name.clone(),
108+
crate_name: crate_name.clone(),
109+
file_name: file_name.clone(),
111110
file_path,
112-
ignored_types,
113-
multi_file,
114-
target_os,
115-
)
116-
.with_context(|| format!("Failed to parse: {file_name}"))?;
111+
};
112+
113+
let parsed_result =
114+
typeshare_core::parser::parse(parse_context, parse_file_context)
115+
.with_context(|| format!("Failed to parse: {file_name}"))?;
117116

118117
if let Some(parsed_data) = parsed_result {
119118
parsed_crates

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "typeshare-core"
3-
version = "1.12.1"
3+
version = "1.13.2"
44
license = "MIT OR Apache-2.0"
55
edition = "2021"
66
description = "The code generator used by Typeshare's command line tool"

core/data/tests/excluded_by_target_os/input.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,14 @@ pub struct AndroidExcluded;
8080
#[typeshare]
8181
#[cfg(all(feature = "my-feature", not(target_os = "ios")))]
8282
pub struct NestedNotTarget1;
83+
84+
/// A struct with no target_os. Should be generated when
85+
/// we use --target-os.
86+
#[typeshare]
87+
pub struct AlwaysAccept;
88+
89+
#[typeshare]
90+
pub enum AlwaysAcceptEnum {
91+
Variant1,
92+
Variant2,
93+
}

core/data/tests/excluded_by_target_os/output.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package proto
22

33
import "encoding/json"
44

5+
// A struct with no target_os. Should be generated when
6+
// we use --target-os.
7+
type AlwaysAccept struct {
8+
}
59
type DefinedTwice struct {
610
Field1 string `json:"field1"`
711
}
@@ -15,6 +19,11 @@ type NestedNotTarget1 struct {
1519
}
1620
type OtherExcluded struct {
1721
}
22+
type AlwaysAcceptEnum string
23+
const (
24+
AlwaysAcceptEnumVariant1 AlwaysAcceptEnum = "Variant1"
25+
AlwaysAcceptEnumVariant2 AlwaysAcceptEnum = "Variant2"
26+
)
1827
type SomeEnum string
1928
const (
2029
)

0 commit comments

Comments
 (0)