|
| 1 | +using DataStructures |
| 2 | +import JSON |
| 3 | + |
| 4 | +# Create package directories |
| 5 | +mkpath("julia/src") |
| 6 | +mkpath("julia/test") |
| 7 | + |
| 8 | +# Copy library to Julia package |
| 9 | +cp(abspath("../lib"),abspath("julia/src/lib"),force=true) |
| 10 | + |
| 11 | +# Copy README and LICENSE |
| 12 | +cp("../README.md", "julia/README.md",force=true) |
| 13 | +cp("../LICENSE", "julia/LICENSE",force=true) |
| 14 | + |
| 15 | +# Get current version number in git: |
| 16 | +version = split(read(`git describe --tags`,String),"-")[1][2:end] |
| 17 | + |
| 18 | +# Create Project.toml |
| 19 | +iop = open(abspath("julia/Project.toml"), "w") |
| 20 | +println(iop,"name = \"LibDLF\"") |
| 21 | +println(iop,"uuid = \"f0c3f387-4ff6-435f-9d63-77e28b8d1347\"") |
| 22 | +println(iop,"authors = [\"The emsig community <info@emsig.xyz> \"]") |
| 23 | +println(iop,"version = \"$version\"") #kwk debug: how to get version in github build? |
| 24 | +println(iop,"\n[deps]") |
| 25 | +println(iop,"DelimitedFiles = \"8bb1440f-4735-579b-a4ab-409b98df4dab\"") |
| 26 | +println(iop,"\n[extras]") |
| 27 | +println(iop,"Test = \"8dfed614-e22c-5e08-85e1-65c5234f0b40\"") |
| 28 | +println(iop,"\n[targets]") |
| 29 | +println(iop,"test = [\"Test\"]") |
| 30 | +close(iop) |
| 31 | + |
| 32 | +# Read in .json file listing all filters |
| 33 | +filters = JSON.parsefile(abspath("julia/src/lib/filters.json"), |
| 34 | + dicttype=DataStructures.OrderedDict) |
| 35 | + |
| 36 | +# Create Julia module |
| 37 | +iol = open(abspath("julia/src/LibDLF.jl"), "w") |
| 38 | + |
| 39 | +# Module name |
| 40 | +println(iol,"module LibDLF\n") |
| 41 | + |
| 42 | +# Create LibDLF.jl files |
| 43 | +for type in filters.keys |
| 44 | + |
| 45 | + stype = titlecase(type) |
| 46 | + |
| 47 | + # Include sub module file in parent |
| 48 | + println(iol, "include(\"$stype.jl\")") |
| 49 | + |
| 50 | + # Create sub module file for filter type |
| 51 | + iot = open(abspath("julia/src/$stype.jl"), "w") |
| 52 | + |
| 53 | + println(iot, "module $stype\n") |
| 54 | + |
| 55 | + # Add used modules |
| 56 | + println(iot, "using DelimitedFiles") |
| 57 | + |
| 58 | + # Add library path variable: |
| 59 | + println(iot, "\nlibpath = @__DIR__") |
| 60 | + |
| 61 | + # Add cache: |
| 62 | + println(iot, "\ncache = Dict() # local cache for any filters already loaded") |
| 63 | + |
| 64 | + # Add filter functions: |
| 65 | + for filt in filters[type] |
| 66 | + |
| 67 | + # Get and write header as docstring: |
| 68 | + iof = open(abspath("julia/src/" * filt["file"]), "r") |
| 69 | + |
| 70 | + # Title |
| 71 | + println(iot, "\n\"\"\"") |
| 72 | + |
| 73 | + sname = filt["name"] |
| 74 | + println(iot, "\t $sname()\n") |
| 75 | + |
| 76 | + println(iot, readline(iof)[2:end]) |
| 77 | + |
| 78 | + # Get vals and preformat if sin & cos |
| 79 | + vals = replace(filt["values"], "," => ", ") |
| 80 | + # println(typeof(vals)) |
| 81 | + if type == "fourier" |
| 82 | + vals = replace(vals, "cos" => "fcos") |
| 83 | + vals = replace(vals, "sin" => "fsin") |
| 84 | + end |
| 85 | + |
| 86 | + # Rest of header |
| 87 | + for line in eachline(iof) |
| 88 | + |
| 89 | + # Do not copy the title-underline; just newline |
| 90 | + if contains(line, "========") |
| 91 | + println(iot, "") |
| 92 | + |
| 93 | + # Empty lines: only remove comment |
| 94 | + elseif line == "#\n" |
| 95 | + println(iot, "") |
| 96 | + |
| 97 | + # The license is the last thing of the header |
| 98 | + elseif contains(line, "This file is part of libdlf") |
| 99 | + |
| 100 | + # Add returned vals |
| 101 | + println(iot, "# Returns\n") |
| 102 | + println(iot, "base, $vals :: Array{Float64,1}") |
| 103 | + println(iot, "Filter base and its values.\n") |
| 104 | + |
| 105 | + # Example |
| 106 | + println(iot, "# Example\n") |
| 107 | + println(iot, "```julia") |
| 108 | + println(iot, "base, $vals = LibDLF.$stype.$sname()") |
| 109 | + println(iot, "```") |
| 110 | + |
| 111 | + # Finish header |
| 112 | + println(iot, "\n\"\"\"") |
| 113 | + |
| 114 | + # Stop header loop |
| 115 | + break |
| 116 | + |
| 117 | + # Print entire line |
| 118 | + else |
| 119 | + println(iot, line[2:end]) |
| 120 | + |
| 121 | + end |
| 122 | + |
| 123 | + end |
| 124 | + |
| 125 | + println(iot, "function $sname()") |
| 126 | + |
| 127 | + println(iot,"\tif !haskey(cache,\"$sname\") # read and add to cache") |
| 128 | + sfile = filt["file"] |
| 129 | + println(iot,"\t\tsfile = joinpath(libpath,\"$sfile\")") |
| 130 | + println(iot, "\t\tdat = readdlm(sfile,comments=true)") |
| 131 | + println(iot,"\t\tcache[\"$sname\"]= tuple([dat[:,c] for c in 1:size(dat,2)]...)") |
| 132 | + println(iot, "\tend") |
| 133 | + println(iot, "\treturn cache[\"$sname\"]") |
| 134 | + println(iot, "end") |
| 135 | + |
| 136 | + # Close file |
| 137 | + close(iof) |
| 138 | + |
| 139 | + end |
| 140 | + |
| 141 | + # Close filter type sub module: |
| 142 | + println(iot, "\nend") |
| 143 | + close(iot) |
| 144 | +end |
| 145 | + |
| 146 | +# Close LibDLF module: |
| 147 | +println(iol,"\nend") |
| 148 | +close(iol) |
| 149 | + |
| 150 | +# Create testing routine |
| 151 | +ior = open(abspath("julia/test/runtests.jl"), "w") |
| 152 | +println(ior,"using LibDLF") |
| 153 | +println(ior,"using Test\n") |
| 154 | +println(ior,"# insert code for @testset blocks and @test unit tests ") |
| 155 | +close(ior) |
0 commit comments