Skip to content

Commit 12f1013

Browse files
Add support for several other file types
Adds support for Dockerfiles, Gemfiles, Ruby files, and Groovy files. Also, in order to fully support Dockerfile and Gemfiles, a new file extension parser was created to deal with files that do not have an extension.
1 parent 1e1ee45 commit 12f1013

File tree

9 files changed

+87
-6
lines changed

9 files changed

+87
-6
lines changed

main.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,16 @@ func walk(ch chan<- *file, start string) {
135135
func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *copyrightData) error {
136136
var lic []byte
137137
var err error
138-
switch filepath.Ext(path) {
138+
switch fileExtension(path) {
139139
default:
140140
return nil
141141
case ".c", ".h":
142142
lic, err = prefix(tmpl, data, "/*", " * ", " */")
143143
case ".js", ".jsx", ".tsx", ".css", ".tf":
144144
lic, err = prefix(tmpl, data, "/**", " * ", " */")
145-
case ".cc", ".cpp", ".cs", ".go", ".hh", ".hpp", ".java", ".m", ".mm", ".proto", ".rs", ".scala", ".swift", ".dart":
145+
case ".cc", ".cpp", ".cs", ".go", ".hh", ".hpp", ".java", ".m", ".mm", ".proto", ".rs", ".scala", ".swift", ".dart", ".groovy":
146146
lic, err = prefix(tmpl, data, "", "// ", "")
147-
case ".py", ".sh", ".yaml", ".yml":
147+
case ".py", ".sh", ".yaml", ".yml", ".dockerfile", "dockerfile", ".rb", "gemfile":
148148
lic, err = prefix(tmpl, data, "", "# ", "")
149149
case ".el", ".lisp":
150150
lic, err = prefix(tmpl, data, "", ";; ", "")
@@ -178,10 +178,19 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
178178
return ioutil.WriteFile(path, b, fmode)
179179
}
180180

181+
func fileExtension(name string) string {
182+
if v := filepath.Ext(name); v != "" {
183+
return strings.ToLower(v)
184+
}
185+
return strings.ToLower(filepath.Base(name))
186+
}
187+
181188
var head = []string{
182-
"#!", // shell script
183-
"<?xml", // XML declaratioon
184-
"<!doctype", // HTML doctype
189+
"#!", // shell script
190+
"<?xml", // XML declaratioon
191+
"<!doctype", // HTML doctype
192+
"# encoding:", // Ruby encoding
193+
"# frozen_string_literal:", // Ruby interpreter instruction
185194
}
186195

187196
func hashBang(b []byte) []byte {

testdata/expected/Gemfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
puts "Hello world!"

testdata/expected/file.Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM scratch
16+
CMD ["echo", "hello world"]

testdata/expected/file.groovy

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
def dummy_function() {
16+
println "Hello world"
17+
}

testdata/expected/file.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
# Copyright 2016 Google Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
puts "Hello world!"

testdata/initial/Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts "Hello world!"

testdata/initial/file.Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM scratch
2+
CMD ["echo", "hello world"]

testdata/initial/file.groovy

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def dummy_function() {
2+
println "Hello world"
3+
}

testdata/initial/file.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# frozen_string_literal: true
2+
puts "Hello world!"

0 commit comments

Comments
 (0)