Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JuliaFormatter #231

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Configuration file for JuliaFormatter.jl
# For more information, see: https://domluna.github.io/JuliaFormatter.jl/stable/config/

always_for_in = true
always_use_return = true
margin = 80
remove_extra_newlines = true
separate_kwargs_with_semicolon = true
short_to_long_function_def = true
31 changes: 31 additions & 0 deletions .github/workflows/format_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: format-check
on:
push:
branches:
- master
- release-*
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/setup-julia@latest
with:
version: '1'
- uses: actions/checkout@v1
- name: Format check
shell: julia --color=yes {0}
run: |
using Pkg
Pkg.add(PackageSpec(; name = "JuliaFormatter", version = "1"))
using JuliaFormatter
format("src"; verbose = true)
out = String(read(Cmd(`git diff`)))
if isempty(out)
exit(0)
end
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)

13 changes: 10 additions & 3 deletions benchmarks/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function print_help()
julia benchmark.jl --compare master
julia benchmark.jl --compare approach_1
""")
return
end

if length(ARGS) != 2
Expand All @@ -31,13 +32,19 @@ else
const suite = Benchmarks.suite(() -> Xpress.Optimizer())
if ARGS[1] == "--new"
Benchmarks.create_baseline(
suite, ARGS[2]; directory = @__DIR__, verbose = true
suite,
ARGS[2];
directory = @__DIR__,
verbose = true,
)
elseif ARGS[1] == "--compare"
Benchmarks.compare_against_baseline(
suite, ARGS[2]; directory = @__DIR__, verbose = true
suite,
ARGS[2];
directory = @__DIR__,
verbose = true,
)
else
print_help()
end
end
end
4 changes: 2 additions & 2 deletions deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import Downloads
import Libdl

const DEPS_FILE = joinpath(dirname(@__FILE__),"deps.jl")
const DEPS_FILE = joinpath(dirname(@__FILE__), "deps.jl")

function write_deps_file(path)
open(DEPS_FILE,"w") do io
open(DEPS_FILE, "w") do io
return print(io, "const xpressdlpath = \"$(escape_string(path))\"")
end
return
Expand Down
226 changes: 116 additions & 110 deletions scripts/build_param_control_dicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,142 +14,148 @@ if !isfile(FILE)
end

function process(FILE)
lines = readlines(FILE)

lines = readlines(FILE)
out = """
#=

out = """
#=
File automatically generated with script:
Xpress.jl/scripts/build_param_control_dicts.jl

File automatically generated with script:
Xpress.jl/scripts/build_param_control_dicts.jl
Last build: $(now())

Last build: $(now())
$("Optimizer version: $(Xpress.get_version())")

$("Optimizer version: $(Xpress.get_version())")
Banner from lib:
$(Xpress.get_banner())

Banner from lib:
$(Xpress.get_banner())
Banner from header (xprs.h):
$(lines[7])
$(lines[8])

Banner from header (xprs.h):
$(lines[7])
$(lines[8])
=#

=#
"""

"""
controls_start = " * control parameters for XPRSprob"
string_control = "/* String control parameters */"
double_control = "/* Double control parameters */"
integer_control = "/* Integer control parameters */"

controls_start = " * control parameters for XPRSprob"
string_control = "/* String control parameters */"
double_control = "/* Double control parameters */"
integer_control = "/* Integer control parameters */"
attributes_start = " * attributes for XPRSprob "
string_attribute = "/* String attributes */"
double_attribute = "/* Double attributes */"
integer_attribute = "/* Integer attributes */"

attributes_start = " * attributes for XPRSprob "
string_attribute = "/* String attributes */"
double_attribute = "/* Double attributes */"
integer_attribute = "/* Integer attributes */"
the_end = " * control parameters for XPRSmipsolpool"

the_end = " * control parameters for XPRSmipsolpool"
current_definiton = Ignore
current_type = Nothing

current_definiton = Ignore
current_type = Nothing
dict = Dict(
i => Any[] for i in [
(j, k) for j in [Int32, String, Float64],
k in [MainAttribute, MainControl]
]
)

dict = Dict(
i => Any[] for i in [(j,k)
for j in [Int32, String, Float64], k in [MainAttribute, MainControl]]
)

for line in lines
# @show line
if startswith(line, the_end)
break
end
if startswith(line, controls_start)
current_definiton = MainControl
continue
elseif startswith(line, attributes_start)
current_definiton = MainAttribute
continue
elseif startswith(line, string_control) || startswith(line, string_attribute)
current_type = String
continue
elseif startswith(line, double_control) || startswith(line, double_attribute)
current_type = Float64
continue
elseif startswith(line, integer_control) || startswith(line, integer_attribute)
current_type = Int32
continue
elseif startswith(line, "#define") && current_type != Nothing && current_definiton != Ignore
data = split(line)
name = data[2]
# @show data[3]
value = tryparse(Int32, data[3])
if value === nothing
println("failed to parse $line")
for line in lines
# @show line
if startswith(line, the_end)
break
end
if startswith(line, controls_start)
current_definiton = MainControl
continue
elseif startswith(line, attributes_start)
current_definiton = MainAttribute
continue
elseif startswith(line, string_control) ||
startswith(line, string_attribute)
current_type = String
continue
elseif startswith(line, double_control) ||
startswith(line, double_attribute)
current_type = Float64
continue
elseif startswith(line, integer_control) ||
startswith(line, integer_attribute)
current_type = Int32
continue
elseif startswith(line, "#define") &&
current_type != Nothing &&
current_definiton != Ignore
data = split(line)
name = data[2]
# @show data[3]
value = tryparse(Int32, data[3])
if value === nothing
println("failed to parse $line")
continue
end
push!(dict[current_type, current_definiton], (name, value))
continue
else
continue
end
push!(dict[current_type, current_definiton], (name, value))
continue
else
continue
end
end

is_first = true

order = [
(String, MainControl),
(Float64, MainControl),
(Int32, MainControl),
(String, MainAttribute),
(Float64, MainAttribute),
(Int32, MainAttribute),
]

for (tp, mode) in order
vec = dict[(tp, mode)]
if is_first
is_first = false
else
out *= ")\n\n"
end
if tp == String && mode == MainAttribute
out *= "const STRING_ATTRIBUTES = Dict{String, Int32}(\n"
elseif tp == Int32 && mode == MainAttribute
out *= "const INTEGER_ATTRIBUTES = Dict{String, Int32}(\n"
elseif tp == Float64 && mode == MainAttribute
out *= "const DOUBLE_ATTRIBUTES = Dict{String, Int32}(\n"
elseif tp == String && mode == MainControl
out *= "const STRING_CONTROLS = Dict{String, Int32}(\n"
elseif tp == Int32 && mode == MainControl
out *= "const INTEGER_CONTROLS = Dict{String, Int32}(\n"
elseif tp == Float64 && mode == MainControl
out *= "const DOUBLE_CONTROLS = Dict{String, Int32}(\n"
end
for (name, value) in vec
out *= " \"$(replace(string(name), "XPRS_"=>""))\" => $value,\n"
is_first = true

order = [
(String, MainControl),
(Float64, MainControl),
(Int32, MainControl),
(String, MainAttribute),
(Float64, MainAttribute),
(Int32, MainAttribute),
]

for (tp, mode) in order
vec = dict[(tp, mode)]
if is_first
is_first = false
else
out *= ")\n\n"
end
if tp == String && mode == MainAttribute
out *= "const STRING_ATTRIBUTES = Dict{String, Int32}(\n"
elseif tp == Int32 && mode == MainAttribute
out *= "const INTEGER_ATTRIBUTES = Dict{String, Int32}(\n"
elseif tp == Float64 && mode == MainAttribute
out *= "const DOUBLE_ATTRIBUTES = Dict{String, Int32}(\n"
elseif tp == String && mode == MainControl
out *= "const STRING_CONTROLS = Dict{String, Int32}(\n"
elseif tp == Int32 && mode == MainControl
out *= "const INTEGER_CONTROLS = Dict{String, Int32}(\n"
elseif tp == Float64 && mode == MainControl
out *= "const DOUBLE_CONTROLS = Dict{String, Int32}(\n"
end
for (name, value) in vec
out *= " \"$(replace(string(name), "XPRS_"=>""))\" => $value,\n"
end
end
end
out *= ")\n\n"
out *= ")\n\n"

out *= """
const STRING_CONTROLS_VALUES = values(STRING_CONTROLS)
out *= """
const STRING_CONTROLS_VALUES = values(STRING_CONTROLS)

const DOUBLE_CONTROLS_VALUES = values(DOUBLE_CONTROLS)
const DOUBLE_CONTROLS_VALUES = values(DOUBLE_CONTROLS)

const INTEGER_CONTROLS_VALUES = values(INTEGER_CONTROLS)
const INTEGER_CONTROLS_VALUES = values(INTEGER_CONTROLS)

const STRING_ATTRIBUTES_VALUES = values(STRING_ATTRIBUTES)
const STRING_ATTRIBUTES_VALUES = values(STRING_ATTRIBUTES)

const DOUBLE_ATTRIBUTES_VALUES = values(DOUBLE_ATTRIBUTES)
const DOUBLE_ATTRIBUTES_VALUES = values(DOUBLE_ATTRIBUTES)

const INTEGER_ATTRIBUTES_VALUES = values(INTEGER_ATTRIBUTES)
"""
const INTEGER_ATTRIBUTES_VALUES = values(INTEGER_ATTRIBUTES)
"""

open(joinpath(@__DIR__, "..", "src", "attributes_controls.jl"), "w") do f
write(f, out)
end
open(joinpath(@__DIR__, "..", "src", "attributes_controls.jl"), "w") do f
return write(f, out)
end

return nothing
return nothing
end

process(FILE);
process(FILE);
Loading
Loading