volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Support generating non `.go` file #285

Closed choonkeat closed 6 years ago

choonkeat commented 6 years ago

At the moment, sqlboiler has strong assumptions that we are generating .go files.

We could potentially leverage sqlboiler to generate graphql schema files or React js apps.

Currently https://github.com/choonkeat/hellocrud project uses code comments to generate what we need and then post process the generated go files.

One possibility is to accept an optional cli flag -ext; for any non .go value, we simply skip the go lang headers and stuff.

saulortega commented 6 years ago

After https://github.com/volatiletech/sqlboiler/issues/303, I see now this case of use.

There should be an option to generate separate files with a custom name and output directory.

Perhaps setting special comments at beginning of the tpl file, for example:

// FILENAME=my_file.js // OUTDIR=/path/to/some/dir

aarondl commented 6 years ago

@saulortega The custom output directory is a harder sell. I don't think it's particularly difficult to move those files after the fact, no? I'm thinking about working on this today so I'd like to know what's deemed acceptable.

aarondl commented 6 years ago

My plan for this currently is going to be:

  1. Collect all files based on extension (.go.tpl, .js.tpl), everything named with .tpl will be processed.
  2. .go files get special stuff (imports + gofmt), other files are rendered as is

In terms of organization and folders, I think I want to do this:

  1. Under the templates/ directory all files will be concatenated into a single file for the model (per extension)
  2. If there is a singleton directory, that directory's files will be put into the parent output directory without any concatenation
  3. Subfolders will repeat this process.

Here's an example:

templates
|- 00_struct.go.tpl
|- 00_struct.ts.js.tpl
|- css
   |- singleton
      |- some_tests.css.tpl
   |- 00_struct.css.tpl
|- singleton
   |- some_tests.go.tpl

Output:

outputdir
|- users.go
|- users.ts.js
|- some_tests.go
|- css
   |- users.css
   |- some_tests.css

Will probably look at implementing this soon unless there's objections to the proposal. @choonkeat, @saulortega Let me know, this must be done before v3 releases because it's technically a breaking change (template file names change).

saulortega commented 6 years ago

I like your plan. I have no objections. :+1:

saulortega commented 6 years ago

However, let me insist on the output directory. :)

Maybe, in subfolders there could be a special file that contains the output directory path. Maybe a JSON string.

aarondl commented 6 years ago

I'm not sure I understand why that's desirable. Take the example we had before:

outputdir
|- users.go
|- users.ts.js
|- some_tests.go
|- css
   |- users.css
   |- some_tests.css

In order to "control output directory" you simply have to do the following:

rm -rf wherever/you/want/it # Effectively --wipe
mv models/css wherever/you/want/it

Because you control the local output directory, it's easy to then just move that/the files inside it wherever you'd like. It doesn't seem worth it to add the extra interface because it's not an easy thing to specify. JSON Files and magic comments both feel bad. Command line flags are difficult because there's a whole directory tree you could specify. Just not seeing the value.

saulortega commented 6 years ago

Okay that's fine. :+1:

aarondl commented 6 years ago

Done in rc2 (v3.0.0-rc2). I really hate how the implementation came together, it's actually horrible. But the actual feature is ok. Works as described above, there's a section in the readme for it.