Skip to content

Commit

Permalink
precompile_enclusure
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Feb 11, 2020
1 parent d9898a1 commit 8fe7890
Showing 1 changed file with 74 additions and 2 deletions.
76 changes: 74 additions & 2 deletions src/bot/precompileInclude.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,80 @@ function precompile_deactivator(package_path::String, precompile_path::String)
#_precompile_()
""")
end
end

"""
precompile_enclusure(package_path::String, precompile_path::String)
Writes a `@static` enclosure to contain inclusion of precompile scripts
# Examples
```julia
examples
```
"""
function precompile_enclusure(package_path::String, precompile_path::String)
if !isfile(package_path)
error("$package_path file doesn't exist")
end

# read package
package_text = Base.read(package_path, String)

enclosure_regex = r"^\@static\s+if\s+should_precompile[\s\S]*end\s*#\s*precompile_enclusure$"

if occursin(enclosure_regex, package_text) # has enclosure

else # no enclosure

if occursin("_precompile_()",package_text) # precompiles without enclosure!
error("Please remove `_precompile_()` and any code that includes a `_precompile_` function from $package_path")
end

# write enclosure
@info "SnoopCompile will try to write some code before end of the module in $package_path"

## find end of a module
# assume last end is the end of a module
package_lines = Base.open(package_path) do io
Base.readlines(io, keep=true)
end

endline = length(package_lines)
for iLine = endline:1
if any(occursin.(["end # module", "end"], Ref(package_lines[iLine])))
endline = iLine
break
end
end

# Run user editor; if something goes wrong, just quit out
try
enclusure = """
# don't edit the following!
# precompile_enclusre
should_precompile = true
@static if should_precompile
#SnoopCompile_find_me
end # precompile_enclusure
"""
insert!(lines,iLine-1,enclusure) # add new empty line before the end
catch e
@error("Error occured during writing", e)
return nothing
end

# write the lines
if lines != nothing
open(package_path, "w") do io
for l in lines
write(io, l)
end
end
end
end
end
################################################################
"""
detectOS()
Expand All @@ -146,5 +220,3 @@ allos = [Sys.iswindows,
end

################################################################

end

0 comments on commit 8fe7890

Please sign in to comment.