Skip to content

Fix : Generate No Files or All Files#23

Merged
cophilot merged 8 commits intocophilot:devfrom
RounakJoshi09:rounakj/files-acid
Feb 15, 2025
Merged

Fix : Generate No Files or All Files#23
cophilot merged 8 commits intocophilot:devfrom
RounakJoshi09:rounakj/files-acid

Conversation

@RounakJoshi09
Copy link
Copy Markdown
Collaborator

Replace the ... with your own content!

Type of change

  • Bug fix
  • New feature
  • New test
  • Refactoring / code cleanup
  • Documentation
  • Other (please describe here: ...)

Description

Previously, an issue occurred when generating files from a template. If the generation process failed for any reason, users were left with an inconsistent state, where some files were generated while others were not. I have resolved this issue by implementing a tracking system that monitors all created files during the generation process. If the process fails midway, all generated files will be removed, ensuring the state remains consistent and as it was before the process started, thereby preventing any inconsistency.

Changes

I created a vector of tuples that contain information about the path of each file or directory created, as well as a flag indicating whether it is a directory. If the generation process fails, the system will iterate through the created paths, check their existence, and remove them based on whether they are directories or files. This ensures that the state remains consistent and free from partial generation artifacts.

  • ...
  • ...
  • Image after failed execution of generate command
    image

Where we will create all files at once - 1.0.0
@RounakJoshi09
Copy link
Copy Markdown
Collaborator Author

RounakJoshi09 commented Jan 20, 2025

Hi @cophilot , I tried of solving #20 , in this PR, the approach I followed is fairly straight forward (explained in the PR), but I think is good enough to get the job done. Please go through the approach, if it fits. Also if you want me to include any tests to test this functionality , please let me know, I will include that.
Thanks

@cophilot
Copy link
Copy Markdown
Owner

Hi @cophilot , I tried of solving #20 , in this PR, the approach I followed is fairly straight forward (explained in the PR), but I think is good enough to get the job done. Please go through the approach, if it fits. Also if you want me to include any tests to test this functionality , please let me know, I will include that. Thanks

Hi @RounakJoshi09
As we already discussed in #20 I don`t like that the files get written out and then cleaned up afterwards. I want that no files are getting generated, at any point, if not all files could be generated. Therefore I would suggest to first loop over the files and check if all files would theoretically be generated (without actually generating them!) and if that is successful for all files then all will be generated. When even one check fails then no file should be generated.

Also a test for that would be highly appreciated.

If you need any help or more information on this let me know :)

@cophilot cophilot marked this pull request as draft January 21, 2025 16:08
@cophilot cophilot linked an issue Jan 21, 2025 that may be closed by this pull request
@cophilot
Copy link
Copy Markdown
Owner

cophilot commented Feb 3, 2025

@RounakJoshi09 If you are finished with the implementation, please set the PR back to ready.

@RounakJoshi09 RounakJoshi09 marked this pull request as ready for review February 3, 2025 15:37
@RounakJoshi09 RounakJoshi09 force-pushed the rounakj/files-acid branch 2 times, most recently from 9aee8ed to f8e8d60 Compare February 3, 2025 17:14
@RounakJoshi09
Copy link
Copy Markdown
Collaborator Author

RounakJoshi09 commented Feb 3, 2025

Hi @cophilot , I have changed the approach , according to which, I am storing all the files to be generated in vector, and when the program confirms that all the files can be generated then I am generating them by iterating through the array. Please check this once and let me know what you think about it.
Also I am not able to frame how can I write test case for this particular scenario, a little help for that would be very helpful.
Thanks,

@cophilot cophilot changed the base branch from master to dev February 4, 2025 16:29
force,
&mut files_to_create,
) {
for file in files_to_create {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior should be handled within the generate_template_dir and similar functions. I would suggest that you do the same behavior within the functions, but you generate the vector within the function that do the checks and if it was successful then generate the files within the function. Here a pseudo example:

fn generate_template_dir(...){
    let mut files_to_create: Vec<FileToCreate> = Vec::new();
     
    for f in files{
       if canBeAdded -> files_to_create.add(f)
       else return false
    }

    // generate the files from files_to_create
}
    

@cophilot
Copy link
Copy Markdown
Owner

cophilot commented Feb 4, 2025

Thanks @RounakJoshi09 ! It already looks pretty good, only one minor refactoring and then its perfect :)

For the test you can edit the tests/command_tests/generate_tests and there you can do something like:

    fs::templates_dir()
        .dir("MyTest")
        .file(".templify.yml").create();
    fs::templates_dir()
        .dir("MyTest")
        .file("test.txt").create();
    fs::templates_dir()
        .dir("MyTest")
        .file("$$name$$.txt").create();

    utils::run_successfully("tpy generate MyTest test1");

    utils::run_fail("tpy generate MyTest test2"); // This should not work because test.txt already exist

    log::contains_line("File ./test.txt already exists."); // check if reason is given to the user

    fs::file("test2.txt").check_not_exist(); // check that the other file was not generated even it would be possible

If you still have issues creating the test case for this then let me know and I will help you.

@cophilot cophilot marked this pull request as draft February 4, 2025 16:56
@RounakJoshi09 RounakJoshi09 marked this pull request as ready for review February 15, 2025 05:56
@RounakJoshi09
Copy link
Copy Markdown
Collaborator Author

Thank you for the feedback, @cophilot . I've implemented the suggested changes. I'd like to provide some context for one of the modifications:

To integrate the file generation process within the template handler, I introduced an additional function. This new function initializes the vector for files and subsequently triggers the file generation. The rationale behind this approach is that generate_template_dir is a recursive function. To ensure all files are properly stored without reinitialization, we need to pass the files vector by reference. Consequently, I created a separate function within the template handler that internally invokes generate_template_dir.

Please let me know if you need any further clarification or have additional suggestions.

@cophilot
Copy link
Copy Markdown
Owner

Hi @RounakJoshi09 Thank you for the fix. I will merge it now and the fix will be released with version 2.0.1 :)

@cophilot cophilot merged commit a380345 into cophilot:dev Feb 15, 2025
1 check passed
@RounakJoshi09
Copy link
Copy Markdown
Collaborator Author

Thanks a lot @cophilot . Can you please assign some new task as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate all files or no files

2 participants