Unverified Commit 11de3f66 authored by Zelda Hessler's avatar Zelda Hessler Committed by GitHub
Browse files

update: canonicalize repo paths (#1016)

* update: canonicalize repo paths
update: fix path log text alignment

* add: more logging for HandwrittenFiles-related functions
add: warning for 'generated' handwritten dotfile

* fix: warning for 'generated' handwritten dotfile
parent d9ffac3c
Loading
Loading
Loading
Loading
+39 −8
Original line number Diff line number Diff line
@@ -12,20 +12,32 @@ use std::path::{Path, PathBuf};
pub static HANDWRITTEN_DOTFILE: &str = ".handwritten";

pub fn delete_all_generated_files_and_folders(directory: &Path) -> anyhow::Result<()> {
    eprintln!("\tchecking for 'generated' files and folders in the current SDK...");
    let dotfile_path = directory.join(HANDWRITTEN_DOTFILE);
    eprintln!("\tloading dotfile at {}", dotfile_path.display());
    let handwritten_files = HandwrittenFiles::from_dotfile(&dotfile_path).context(here!())?;

    for path in handwritten_files
    let generated_files = handwritten_files
        .generated_files_and_folders_iter(directory)
        .context(here!())?
    {
        .context(here!())?;

    let mut file_count = 0;
    let mut folder_count = 0;

    for path in generated_files {
        if path.is_file() {
            std::fs::remove_file(path)?
            std::fs::remove_file(path)?;
            file_count += 1;
        } else if path.is_dir() {
            std::fs::remove_dir_all(path)?
            std::fs::remove_dir_all(path)?;
            folder_count += 1;
        };
    }

    eprintln!(
        "\tdeleted {} 'generated' files and {} folders in the current SDK folder",
        file_count, folder_count
    );

    Ok(())
}

@@ -33,14 +45,21 @@ pub fn find_handwritten_files_and_folders(
    aws_sdk_path: &Path,
    build_artifacts_path: &Path,
) -> anyhow::Result<Vec<PathBuf>> {
    eprintln!("\tchecking for 'handwritten' files and folders in the generated SDK folder...");
    let dotfile_path = aws_sdk_path.join(HANDWRITTEN_DOTFILE);
    eprintln!("\tloading dotfile at {}", dotfile_path.display());
    let handwritten_files = HandwrittenFiles::from_dotfile(&dotfile_path).context(here!())?;

    let files = handwritten_files
    let files: Vec<_> = handwritten_files
        .handwritten_files_and_folders_iter(build_artifacts_path)
        .context(here!())?
        .collect();

    eprintln!(
        "\tfound {} 'handwritten' files and folders in the generated SDK folder",
        files.len()
    );

    Ok(files)
}

@@ -83,8 +102,20 @@ enum FileKind {
impl<'file> HandwrittenFiles<'file> {
    pub fn from_dotfile(dotfile_path: &'file Path) -> Result<Self, HandwrittenFilesError> {
        let files_and_folders = gitignore::File::new(dotfile_path)?;
        let handwritten_files = Self { files_and_folders };

        let dotfile_is_marked_as_handwritten = handwritten_files
            .is_handwritten(Path::new(HANDWRITTEN_DOTFILE))
            .expect("file must exist because we just checked it");

        if !dotfile_is_marked_as_handwritten {
            eprintln!(
                "warning: your handwritten dotfile at {} isn't marked as handwritten, is this intentional?",
                dotfile_path.display()
            );
        }

        Ok(Self { files_and_folders })
        Ok(handwritten_files)
    }

    pub fn is_handwritten(&self, path: &'file Path) -> Result<bool, HandwrittenFilesError> {
+13 −15
Original line number Diff line number Diff line
@@ -72,22 +72,20 @@ fn main() -> Result<()> {

/// 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, branch: &str) -> Result<()> {
    eprintln!(
        "aws-sdk-rust path:\t{}",
        aws_sdk.canonicalize().context(here!())?.display()
    );
    eprintln!(
        "smithy-rs path:\t{}",
        smithy_rs.canonicalize().context(here!())?.display()
    );
    // In case these are relative paths, canonicalize them into absolute paths
    let aws_sdk = aws_sdk.canonicalize().context(here!())?;
    let smithy_rs = smithy_rs.canonicalize().context(here!())?;

    eprintln!("aws-sdk-rust path:\t{}", aws_sdk.display());
    eprintln!("smithy-rs path:\t\t{}", smithy_rs.display());

    // Open the repositories we'll be working with
    let smithy_rs_repo = Repository::open(smithy_rs).context("couldn't open smithy-rs repo")?;
    let aws_sdk_repo = Repository::open(aws_sdk).context("couldn't open aws-sdk-rust repo")?;
    let smithy_rs_repo = Repository::open(&smithy_rs).context("couldn't open smithy-rs repo")?;
    let aws_sdk_repo = Repository::open(&aws_sdk).context("couldn't open aws-sdk-rust repo")?;

    // Check repo that we're going to be moving the code into to see what commit it was last synced with
    let last_synced_commit =
        get_last_synced_commit(aws_sdk).context("couldn't get last synced commit")?;
        get_last_synced_commit(&aws_sdk).context("couldn't get last synced commit")?;
    let commit_revs = commits_to_be_applied(&smithy_rs_repo, &last_synced_commit)
        .context("couldn't build list of commits that need to be synced")?;

@@ -124,13 +122,13 @@ fn sync_aws_sdk_with_smithy_rs(smithy_rs: &Path, aws_sdk: &Path, branch: &str) -
            )
        })?;

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

        // Check that we aren't generating any files that we've marked as "handwritten"
        let handwritten_files_in_generated_sdk_folder =
            find_handwritten_files_and_folders(aws_sdk, &build_artifacts)?;
            find_handwritten_files_and_folders(&aws_sdk, &build_artifacts)?;
        if !handwritten_files_in_generated_sdk_folder.is_empty() {
            bail!(
                "found one or more 'handwritten' files/folders in generated code: {:#?}\nhint: if this file is newly generated, remove it from .handwritten",
@@ -138,7 +136,7 @@ fn sync_aws_sdk_with_smithy_rs(smithy_rs: &Path, aws_sdk: &Path, branch: &str) -
            );
        }

        copy_sdk(&build_artifacts, aws_sdk)?;
        copy_sdk(&build_artifacts, &aws_sdk)?;
        create_mirror_commit(&aws_sdk_repo, &commit)
            .context("couldn't commit SDK changes to aws-sdk-rust")?;
    }