Unverified Commit 32923a33 authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Include examples in SDK auto-sync (#1121)

parent e0f01933
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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.
///
+44 −5
Original line number Diff line number Diff line
@@ -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,
@@ -65,11 +68,18 @@ 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)
    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"))
}

@@ -77,11 +87,13 @@ fn main() -> Result<()> {
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!())?;
@@ -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")?;

@@ -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!(
@@ -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");
@@ -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"],