Skip to content

Commit

Permalink
feat: add mise and node engines support (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde authored Sep 29, 2024
1 parent 6f8e449 commit d6c1b0f
Show file tree
Hide file tree
Showing 39 changed files with 337 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ Read on to see runtime-specific examples and how to configure the generated Dock

#### Detected Files
- `bun.lockb`
- `bun.lock`
- `bunfig.toml`

#### Version Detection
- `.tool-versions` - `bun {VERSION}`
- `.mise.toml` - `bun = "{VERSION}"`

#### Runtime Image
`oven/bun:${VERSION}-slim`
Expand Down Expand Up @@ -174,6 +176,7 @@ Detected in order of precedence:

#### Version Detection
- `.tool-versions` - `deno {VERSION}`
- `.mise.toml` - `deno = "{VERSION}"`

#### Runtime Image
`debian:stable-slim`
Expand Down Expand Up @@ -209,6 +212,7 @@ Detected in order of precedence:
- `.tool-versions` - `erlang {VERSION}`
- `.elixir-version` - `{VERSION}`
- `.erlang-version` - `{VERSION}`
- `.mise.toml` - `erlang = "{VERSION}"`

#### Runtime Image
`debian:stable-slim`
Expand All @@ -233,6 +237,7 @@ Detected in order of precedence:

#### Version Detection
- `.tool-versions` - `golang {VERSION}`
- `.mise.toml` - `go = "{VERSION}"`
- `go.mod` - `go {VERSION}`

#### Runtime Image
Expand Down Expand Up @@ -309,6 +314,8 @@ Maven version:
- `.tool-versions` - `nodejs {VERSION}`
- `.nvmrc` - `v{VERSION}`
- `.node-version` - `v{VERSION}`
- `.mise.toml` - `node = "{VERSION}"`
- `package.json` - `"engines": {"node": "{VERSION}"}`

#### Runtime Image
`node:${VERSION}-slim`
Expand Down Expand Up @@ -437,6 +444,7 @@ In order of precedence:
#### Version Detection
- `.tool-versions` - `python {VERSION}`
- `.python-version` - `{VERSION}`
- `.mise.toml` - `python = "{VERSION}"`
- `runtime.txt` - `python-{VERSION}`

#### Runtime Image
Expand Down Expand Up @@ -477,6 +485,7 @@ In order of precedence:
#### Version Detection
- `.tool-versions` - `ruby {VERSION}`
- `.ruby-version` - `{VERSION}`
- `.mise.toml` - `ruby = "{VERSION}"`
- `Gemfile` - `ruby '{VERSION}'`

#### Runtime Image
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/flexstack/new-dockerfile

go 1.21
go 1.21.0

toolchain go1.21.5

require (
github.com/lmittmann/tint v1.0.4
Expand All @@ -10,11 +12,12 @@ require (
)

require (
github.com/Masterminds/semver/v3 v3.3.0
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand All @@ -24,6 +26,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
21 changes: 21 additions & 0 deletions runtime/bun.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"path/filepath"
"strings"
"text/template"

"github.com/pelletier/go-toml/v2"
)

type Bun struct {
Expand All @@ -24,6 +26,7 @@ func (d *Bun) Name() RuntimeName {
func (d *Bun) Match(path string) bool {
checkPaths := []string{
filepath.Join(path, "bun.lockb"),
filepath.Join(path, "bun.lock"),
filepath.Join(path, "bunfig.toml"),
}

Expand Down Expand Up @@ -185,6 +188,7 @@ func findBunVersion(path string, log *slog.Logger) (*string, error) {
version := ""
versionFiles := []string{
".tool-versions",
".mise.toml",
}

for _, file := range versionFiles {
Expand Down Expand Up @@ -214,6 +218,23 @@ func findBunVersion(path string, log *slog.Logger) (*string, error) {
return nil, fmt.Errorf("Failed to read .tool-versions file")
}

case ".mise.toml":
var mise MiseToml
if err := toml.NewDecoder(f).Decode(&mise); err != nil {
return nil, fmt.Errorf("Failed to decode .mise.toml file")
}
bunVersion, ok := mise.Tools["bun"].(string)
if !ok {
versions, ok := mise.Tools["bun"].([]string)
if ok {
bunVersion = versions[0]
}
}
if bunVersion != "" {
version = bunVersion
log.Info("Detected Bun version in .mise.toml: " + version)
break
}
}

f.Close()
Expand Down
5 changes: 5 additions & 0 deletions runtime/bun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func TestBunGenerateDockerfile(t *testing.T) {
path: "../testdata/bun",
expected: []any{`ARG VERSION=1`, `ARG INSTALL_CMD="bun install"`, regexp.MustCompile(`^ARG BUILD_CMD=$`), `ARG START_CMD="bun index.ts"`},
},
{
name: "Bun project w/ mise",
path: "../testdata/bun-mise",
expected: []any{`ARG VERSION=1.1.3`, `ARG INSTALL_CMD="bun install"`, regexp.MustCompile(`^ARG BUILD_CMD=$`), `ARG START_CMD="bun index.ts"`},
},
{
name: "Bun project with .ts file",
path: "../testdata/bun-bunfig",
Expand Down
22 changes: 22 additions & 0 deletions runtime/deno.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"path/filepath"
"strings"
"text/template"

"github.com/pelletier/go-toml/v2"
)

type Deno struct {
Expand Down Expand Up @@ -209,6 +211,7 @@ func findDenoVersion(path string, log *slog.Logger) (*string, error) {
version := ""
versionFiles := []string{
".tool-versions",
".mise.toml",
}

for _, file := range versionFiles {
Expand Down Expand Up @@ -237,12 +240,31 @@ func findDenoVersion(path string, log *slog.Logger) (*string, error) {
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("Failed to read .tool-versions file")
}

case ".mise.toml":
var mise MiseToml
if err := toml.NewDecoder(f).Decode(&mise); err != nil {
return nil, fmt.Errorf("Failed to decode .mise.toml file")
}
denoVersion, ok := mise.Tools["deno"].(string)
if !ok {
versions, ok := mise.Tools["deno"].([]string)
if ok {
denoVersion = versions[0]
}
}
if denoVersion != "" {
version = denoVersion
log.Info("Detected Deno version in .mise.toml: " + version)
break
}
}

f.Close()
if version != "" {
break
}

}
}

Expand Down
5 changes: 5 additions & 0 deletions runtime/deno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func TestDenoGenerateDockerfile(t *testing.T) {
path: "../testdata/deno",
expected: []any{`ARG VERSION=latest`, `ARG INSTALL_CMD="deno cache main.ts"`, `ARG START_CMD="deno run --allow-all main.ts"`},
},
{
name: "Deno project w/ mise",
path: "../testdata/deno-mise",
expected: []any{`ARG VERSION=1.43.2`, `ARG INSTALL_CMD="deno cache main.ts"`, `ARG START_CMD="deno run --allow-all main.ts"`},
},
{
name: "Deno project with .ts file",
path: "../testdata/deno-jsonc",
Expand Down
26 changes: 23 additions & 3 deletions runtime/elixir.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path/filepath"
"strings"
"text/template"

"github.com/pelletier/go-toml/v2"
)

type Elixir struct {
Expand Down Expand Up @@ -120,9 +122,9 @@ RUN addgroup --system nonroot && adduser --system --ingroup nonroot nonroot
RUN chown -R nonroot:nonroot /app
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
ENV MIX_ENV="prod"
Expand Down Expand Up @@ -210,6 +212,7 @@ func findOTPVersion(path string, log *slog.Logger) (*string, error) {
versionFiles := []string{
".tool-versions",
".erlang-version",
".mise.toml",
}

for _, file := range versionFiles {
Expand Down Expand Up @@ -254,6 +257,23 @@ func findOTPVersion(path string, log *slog.Logger) (*string, error) {
return nil, fmt.Errorf("Failed to read .erlang-version file")
}

case ".mise.toml":
var mise MiseToml
if err := toml.NewDecoder(f).Decode(&mise); err != nil {
return nil, fmt.Errorf("Failed to decode .mise.toml file")
}
erlangVersion, ok := mise.Tools["erlang"].(string)
if !ok {
versions, ok := mise.Tools["erlang"].([]string)
if ok {
erlangVersion = versions[0]
}
}
if erlangVersion != "" {
version = erlangVersion
log.Info("Detected Erlang version in .mise.toml: " + version)
break
}
}

f.Close()
Expand Down
5 changes: 5 additions & 0 deletions runtime/elixir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func TestElixirGenerateDockerfile(t *testing.T) {
path: "../testdata/elixir",
expected: []any{`ARG VERSION=1.10`, `ARG OTP_VERSION=22`, `ARG BIN_NAME=hello`},
},
{
name: "Elixir project w/ mise",
path: "../testdata/elixir-mise",
expected: []any{`ARG VERSION=1.10`, `ARG OTP_VERSION=23`, `ARG BIN_NAME=hello`},
},
{
name: "Elixir project with .tool-versions",
path: "../testdata/elixir-tool-versions",
Expand Down
20 changes: 20 additions & 0 deletions runtime/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path/filepath"
"strings"
"text/template"

"github.com/pelletier/go-toml/v2"
)

type Golang struct {
Expand Down Expand Up @@ -145,6 +147,7 @@ func findGoVersion(path string, log *slog.Logger) (*string, error) {
version := ""
versionFiles := []string{
".tool-versions",
".mise.toml",
"go.mod",
}

Expand Down Expand Up @@ -190,6 +193,23 @@ func findGoVersion(path string, log *slog.Logger) (*string, error) {
return nil, fmt.Errorf("Failed to read go.mod file")
}

case ".mise.toml":
var mise MiseToml
if err := toml.NewDecoder(f).Decode(&mise); err != nil {
return nil, fmt.Errorf("Failed to decode .mise.toml file")
}
goVersion, ok := mise.Tools["go"].(string)
if !ok {
versions, ok := mise.Tools["go"].([]string)
if ok {
goVersion = versions[0]
}
}
if goVersion != "" {
version = goVersion
log.Info("Detected Python version in .mise.toml: " + version)
break
}
}

f.Close()
Expand Down
5 changes: 5 additions & 0 deletions runtime/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func TestGolangGenerateDockerfile(t *testing.T) {
path: "../testdata/go",
expected: []any{`ARG VERSION=1.16.3`, `ARG PACKAGE=./main.go`},
},
{
name: "Golang project w/ mise",
path: "../testdata/go-mise",
expected: []any{`ARG VERSION=1.16`, `ARG PACKAGE=./main.go`},
},
{
name: "Golang project with go.mod file",
path: "../testdata/go-mod",
Expand Down
4 changes: 2 additions & 2 deletions runtime/nextjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1
ENV NEXT_TELEMETRY_DISABLED=1
RUN {{.BuildMounts}}if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
Expand Down Expand Up @@ -222,7 +222,7 @@ COPY --from=builder --chown=nonroot:nonroot /app/node_modules ./node_modules
USER nonroot
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED 1
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=8080
EXPOSE ${PORT}
CMD ["node_modules/.bin/next", "start", "-H", "0.0.0.0"]
Expand Down
Loading

0 comments on commit d6c1b0f

Please sign in to comment.