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

typo generator wipes out directory where it puts the generated code (targetDir) #76

Open
boggye opened this issue Dec 29, 2023 · 4 comments

Comments

@boggye
Copy link

boggye commented Dec 29, 2023

Hi,

I have the following generator:

// remember to give the project a github star if you like it <3
//
//> using dep "com.olvind.typo::typo:0.6.0"
//> using scala "3.3.0"

import typo.*

// adapt to your instance and credentials
implicit val c: java.sql.Connection =
  java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/somedb?user=postgres&password=password")

val options = Options(
  // customize package name for generated code
  pkg = "typo.models",
  // pick your database library
  dbLib = Some(DbLibName.Anorm),
  jsonLibs = List(JsonLibName.PlayJson),
  // many more possibilities for customization here
  // ...
)

// current folder, where you run the script from
val location = java.nio.file.Path.of(sys.props("user.dir"))

// destination folder. All files in this dir will be overwritten!
val targetDir = location.resolve("app")

// where Typo will look for sql files
val scriptsFolder = location.resolve("sql")

// you can use this to customize which relations you want to generate code for, see below
val selector = Selector.ExcludePostgresInternal

generateFromDb(options, folder = targetDir, selector = selector, scriptsPaths = List(scriptsFolder))
  .overwriteFolder()

// add changed files to git, so you can keep them under control
//scala.sys.process.Process(List("git", "add", targetDir.toString)).!!

I have a play framework project and I want to place the generated code in a package under app. As is, the generator wiped out the content of the app folder (it included the controllers code and others) which was somewhat unexpected. I thought the generator would remove the typo folder recursively. Luckily, I use IntelliJ and I was able to restore the deleted code. It is a brand new project and I didn't add the code yet to git until now after this accident.

Are there any options that allow me to tell the generator not to wipe out the content of the app folder, i.e. the content of targetDir?

For now, I am going to add the generated code somewhere under: target/scala-3.3.1\typo.

Thanks

@boggye boggye changed the title typo generator wipes out directory where it puts the generated code typo generator wipes out directory where it puts the generated code (targetDir) Dec 29, 2023
@oyvindberg
Copy link
Owner

oyvindberg commented Dec 29, 2023

Yeah sorry, there was a very explicit warning about this which disappeared in the last documentation update.

I'll update with clear instructions on how to handle this tomorrow hopefully, not on a computer today.

As a workaround just generate the source somewhere else and move it manually

@boggye
Copy link
Author

boggye commented Dec 29, 2023

Just a suggestion, maybe the default should not be to delete the directory.

@oyvindberg
Copy link
Owner

I'm thinking about a better approach here, but haven't found it quite yet. Typo needs to be able to delete outdated files in the folder(s) you tell it you write to, so effectively it needs to control the whole folder and delete unknown files.

What I do is I add another source directory to the build and generate into it. That pattern is solid and would just need to be documented better. But there is still the possibility of data loss if people do the obvious thing

@boggye
Copy link
Author

boggye commented Dec 31, 2023

I ended up placing the code in target/scala-3.3.1/typo/main/scala and I added this path to the source paths in build.sbt with Compile / unmanagedSourceDirectories += baseDirectory.value / "target/scala-3.3.1/typo/main/scala".

I think I am good - we can close this thread.

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

No branches or pull requests

2 participants