Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a folder #4012

Closed
nicolasb29 opened this issue Feb 16, 2024 · 9 comments
Closed

Create a folder #4012

nicolasb29 opened this issue Feb 16, 2024 · 9 comments
Labels
question Further information is requested

Comments

@nicolasb29
Copy link
Contributor

Maybe it's not the goal of openrewrite but :

What problem are you trying to solve?

I want to add a yaml file to a folder that doesn't exist.

Describe the situation before applying the recipe

My folder doesn't exist

Describe the situation after applying the recipe

Folder exist

Have you considered any alternatives or workarounds?

Quick and dirty :

    @Override
    public TreeVisitor<?, ExecutionContext> getVisitor() {
        Path path = Paths.get(relativeFolderName);
        if (!Files.exists(path)) {
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return TreeVisitor.noop();
    }`
@timtebeek
Copy link
Contributor

Hi! The way to do this would be to just create the YAML file from a recipe, and that should result in the folder being created as well; no need to manually create the folder first! :)

Let me know if that doesn't work out for you somehow, and we can look into specifics. I'll monitor any replies here, but will close the issue already as I'm assuming there's nothing left to do.

@timtebeek timtebeek added the question Further information is requested label Feb 16, 2024
@nicolasb29
Copy link
Contributor Author

Hi!

I know why: to run my recipes, I don't use a gradle init but I have my own code, like the example in the past on the "running recipes" url (I made an upgrade to OpenRewrite 8).

On this part of code

Files.writeString(result.getAfter().getSourcePath(),
                                result.getAfter().printAll());

I have an exception Caused by: java.nio.file.NoSuchFileException: foo/bar/baz.yaml

I will search how to solve it.

Thanks.

@timtebeek
Copy link
Contributor

Ah yes would not immediately have expected that; in that case you'll want to check that any parent to sourcePath exists before you write out the file.

Any particular reason you rolled your own solution for running recipes? Curious what case wasn't covered, and if there's something to adopt.

@nicolasb29
Copy link
Contributor Author

In my opinion, It is easier to deploy a jar in a context where there is a lots of developers and Java’s applications. I had the possibility to get recipes from a remote server, so it is transparent for developers (they don’t have to download recipes first and they have always the latest version of them). There is also different option to verbose output when recipes don’t work as expected.

For cases where it is not a Java application context, we don’t need to install Gradle or have a Gradle wrapper (JRE is installed everywhere for us).

And one year ago, when I tried with Gradle, I had some problems so I started to develop my own solution.

@timtebeek
Copy link
Contributor

Ah sorry to hear you had issues with Gradle earlier; curious to know if those persist, as we've made a lot of improvements over the past year.

Just be aware that we use the Maven and Gradle plugins to set markers common to all source files, some of which are used by recipes. You'd have to replicate that and keep it in sync if you roll your own, but you likely will have encountered that already.

@nicolasb29
Copy link
Contributor Author

nicolasb29 commented Feb 16, 2024

What do mean by markers ? FencedMarkerPrinter for example or SanitizedMarkerPrinter in grade plugin?

You have an example of recipe which use them ?

I will test gradle again to know if those persist.

@timtebeek
Copy link
Contributor

timtebeek commented Feb 17, 2024

Ah not I meant the provenance markers we attach to source files. These are for instance used when we upgrade Maven or Gradle wrappers, or when we want to check what Java version is used before running a recipe.

@nicolasb29
Copy link
Contributor Author

Ok ! I didn’t have the problem but I will check, thanks !

@nicolasb29
Copy link
Contributor Author

Hi,

I tested the Gradle version (with init.gradle) and it is working fine ! I made a pull request to the Gradle plugin : openrewrite/rewrite-gradle-plugin#294

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
Archived in project
Development

No branches or pull requests

2 participants