Skip to content

Commit

Permalink
Include examples in SDK auto-sync (#1121)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti authored Jan 26, 2022
1 parent e0f0193 commit 32923a3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
12 changes: 12 additions & 0 deletions tools/smithy-rs-sync/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ pub fn find_handwritten_files_and_folders(
Ok(files)
}

/// Similar to [`std::fs::remove_dir_all`] except that it doesn't error out if
/// the directory to be removed doesn't exist.
pub fn remove_dir_all_idempotent(path: impl AsRef<Path>) -> anyhow::Result<()> {
match std::fs::remove_dir_all(path.as_ref()) {
Ok(_) => Ok(()),
Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => Ok(()),
_ => Err(err).context(here!()),
},
}
}

/// A struct with methods that help when checking to see if a file is handwritten or
/// automatically generated.
///
Expand Down
49 changes: 44 additions & 5 deletions tools/smithy-rs-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ struct Opt {
/// The path to the aws-sdk-rust folder.
#[structopt(long, parse(from_os_str))]
aws_sdk: PathBuf,
/// Path to the aws-doc-sdk-examples repository.
#[structopt(long, parse(from_os_str))]
sdk_examples: PathBuf,
/// The branch in aws-sdk-rust that commits will be mirrored to.
#[structopt(long, default_value = "next")]
branch: String,
Expand Down Expand Up @@ -65,23 +68,32 @@ fn main() -> Result<()> {
let Opt {
smithy_rs,
aws_sdk,
sdk_examples,
branch,
max_commits_to_sync,
} = Opt::from_args();

sync_aws_sdk_with_smithy_rs(&smithy_rs, &aws_sdk, &branch, max_commits_to_sync)
.map_err(|e| e.context("The sync failed"))
sync_aws_sdk_with_smithy_rs(
&smithy_rs,
&aws_sdk,
&sdk_examples,
&branch,
max_commits_to_sync,
)
.map_err(|e| e.context("The sync failed"))
}

/// Run through all commits made to `smithy-rs` since last sync and "replay" them onto `aws-sdk-rust`.
fn sync_aws_sdk_with_smithy_rs(
smithy_rs: &Path,
aws_sdk: &Path,
sdk_examples: &Path,
branch: &str,
max_commits_to_sync: usize,
) -> Result<()> {
let aws_sdk = resolve_git_repo("aws-sdk-rust", aws_sdk)?;
let smithy_rs = resolve_git_repo("smithy-rs", smithy_rs)?;
let sdk_examples = resolve_git_repo("aws-doc-sdk-examples", sdk_examples)?;

// Rebase aws-sdk-rust's target branch on top of main
rebase_on_main(&aws_sdk, branch).context(here!())?;
Expand Down Expand Up @@ -126,7 +138,7 @@ fn sync_aws_sdk_with_smithy_rs(
)
})?;

let build_artifacts = build_sdk(&smithy_rs).context("couldn't build SDK")?;
let build_artifacts = build_sdk(&sdk_examples, &smithy_rs).context("couldn't build SDK")?;
clean_out_existing_sdk(&aws_sdk)
.context("couldn't clean out existing SDK from aws-sdk-rust")?;

Expand Down Expand Up @@ -183,6 +195,10 @@ fn resolve_git_repo(repo: &str, path: &Path) -> Result<PathBuf> {
/// need to be resolved. Since the sync is run regularly, this will catch conflicts
/// before syncing a commit into the target branch.
fn rebase_on_main(aws_sdk_path: &Path, branch: &str) -> Result<()> {
eprintln!(
"Rebasing aws-sdk-rust/{} on top of aws-sdk-rust/main...",
branch
);
let _ = run(&["git", "fetch", "origin", "main"], aws_sdk_path).context(here!())?;
if let Err(err) = run(&["git", "rebase", "origin/main"], aws_sdk_path) {
bail!(
Expand Down Expand Up @@ -245,9 +261,32 @@ fn set_last_synced_commit(repo_path: &Path, oid: &Oid) -> Result<()> {
.with_context(|| format!("Couldn't write commit hash to '{}'", path.display()))
}

/// Place the examples from aws-doc-sdk-examples into the correct place in smithy-rs
/// to be included with the generated SDK.
fn setup_examples(sdk_examples_path: &Path, smithy_rs_path: &Path) -> Result<()> {
let from = sdk_examples_path.canonicalize().context(here!())?;
let from = from.join("rust_dev_preview");
let from = from.as_os_str().to_string_lossy();

eprintln!("\tcleaning examples...");
fs::remove_dir_all_idempotent(smithy_rs_path.join("aws/sdk/examples")).context(here!())?;

eprintln!(
"\tcopying examples from '{}' to 'smithy-rs/aws/sdk/examples'...",
from
);
let _ = run(&["cp", "-r", &from, "aws/sdk/examples"], smithy_rs_path).context(here!())?;
fs::remove_dir_all_idempotent(smithy_rs_path.join("aws/sdk/examples/.cargo"))
.context(here!())?;
std::fs::remove_file(smithy_rs_path.join("aws/sdk/examples/Cargo.toml")).context(here!())?;
Ok(())
}

/// Run the necessary commands to build the SDK. On success, returns the path to the folder containing
/// the build artifacts.
fn build_sdk(smithy_rs_path: &Path) -> Result<PathBuf> {
fn build_sdk(sdk_examples_path: &Path, smithy_rs_path: &Path) -> Result<PathBuf> {
setup_examples(sdk_examples_path, smithy_rs_path).context(here!())?;

eprintln!("\tbuilding the SDK...");
let start = Instant::now();
let gradlew = smithy_rs_path.join("gradlew");
Expand All @@ -256,7 +295,7 @@ fn build_sdk(smithy_rs_path: &Path) -> Result<PathBuf> {
.expect("for our use case, this will always be UTF-8");

// The output of running these commands isn't logged anywhere unless they fail
let _ = run(&["rm", "-rf", "aws/sdk/build"], smithy_rs_path).context(here!())?;
fs::remove_dir_all_idempotent(smithy_rs_path.join("aws/sdk/build")).context(here!())?;
let _ = run(&[gradlew, ":aws:sdk:clean"], smithy_rs_path).context(here!())?;
let _ = run(
&[gradlew, "-Paws.fullsdk=true", ":aws:sdk:assemble"],
Expand Down

0 comments on commit 32923a3

Please sign in to comment.