Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cleaning-portalmec
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Richard Fernando Heise Ferreira
cleaning-portalmec
Commits
c677e1f7
Commit
c677e1f7
authored
7 years ago
by
bfs15
Browse files
Options
Downloads
Patches
Plain Diff
Added script to add header to files, maintaining shebangs
parent
d918e0b5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
insert_header.rb
+82
-0
82 additions, 0 deletions
insert_header.rb
with
82 additions
and
0 deletions
insert_header.rb
0 → 100644
+
82
−
0
View file @
c677e1f7
#!/usr/bin/env ruby
require
'fileutils'
# array of tuples
HEADER_FILENAME
=
0
EXTENSIONS
=
1
headers
=
[
# file with license commented with hashtags # like this
# codes with hashtag comments
[
"license.txt"
,
[
".rb"
,
".yml"
,
".rake"
,
".ru"
,
".sh"
]]
# add other files that use other comment types
# add other extensions that use other comment types
]
# if your code commets aren't hashtags, create other license files
# add new license file and file extensions to the array like this
# headers = [
# ["license.slash.txt", [".ext_with_slash"]]
# ["license.hash.txt", [".ext_with_hash1", ".ext_with_hash2"]]
# ]
# inserts contents of file 'header_filename' into all files of type in the array 'extensions'
def
insert_header_on_filetypes
(
extensions
,
header_filename
)
extensions
.
each
{
|
e
|
Dir
.
glob
(
"
#{
Dir
.
pwd
}
/**/*
#{
e
}
"
).
each
{
|
filename
|
new_f
=
File
.
join
(
File
.
dirname
(
filename
),
File
.
basename
(
header_filename
)
+
".tmp"
)
FileUtils
.
cp
(
header_filename
,
new_f
)
puts
"Appending "
+
filename
+
" into "
+
new_f
File
.
open
(
filename
,
"r"
)
{
|
file
|
line
=
file
.
gets
# if file has shebang on first line
if
line
.
start_with?
(
"#!/"
)
File
.
open
(
new_f
,
"w"
)
{
|
merged_file
|
# adds the original file shebang #!/usr/bin/[..] as first line
merged_file
<<
line
# writes header file
File
.
open
(
header_filename
,
"r"
)
{
|
header_file
|
while
h_line
=
header_file
.
gets
merged_file
<<
h_line
end
}
# writes remaining source file
while
line
=
file
.
gets
merged_file
<<
line
end
}
# if file doesn't have shebang on first line
else
File
.
open
(
new_f
,
"a"
)
{
|
merged_file
|
# appends source file to header_file
merged_file
<<
line
while
line
=
file
.
gets
merged_file
<<
line
end
}
end
}
puts
"Overwriting
\"
"
+
new_f
+
"
\"
into "
+
filename
File
.
rename
(
new_f
,
filename
)
puts
"done
\n\n
"
}
}
end
def
checkFile
(
path
)
if
!
File
.
file?
(
path
)
puts
"Header file specified
#{
File
.
basename
(
path
)
}
does not exist on current directory, please create one"
exit
end
end
headers
.
each
{
|
header
|
checkFile
(
header
[
HEADER_FILENAME
])
insert_header_on_filetypes
(
header
[
EXTENSIONS
],
header
[
HEADER_FILENAME
])
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment