Skip to content

Commit

Permalink
Adding package manager functions to OMJulia.API (#124)
Browse files Browse the repository at this point in the history
- installPackage
  - updatePackageIndex
  - getAvailablePackageVersions
  - upgradeInstalledPackages
  • Loading branch information
AnHeuermann authored May 27, 2024
1 parent 35807b4 commit ed8ac12
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OMJulia"
uuid = "0f4fe800-344e-11e9-2949-fb537ad918e1"
authors = ["Martin Sjölund <[email protected]>", "Arunkumar Palanisamy <[email protected]>"]
version = "0.3.1"
version = "0.3.2"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
2 changes: 2 additions & 0 deletions src/OMJulia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ module OMJulia
export linearize, getLinearInputs, getLinearOutputs, getLinearStates, getLinearizationOptions, setLinearizationOptions
# sensitivity analysis
export sensitivity
# package manager
export installPackage, updatePackageIndex, getAvailablePackageVersions, upgradeInstalledPackages

include("error.jl")
include("parser.jl")
Expand Down
82 changes: 81 additions & 1 deletion src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ module API
end

"""
instantiateModel(omc, className)
instantiateModel(omc, className)
Instantiates the class and returns the flat Modelica code.
Expand All @@ -549,4 +549,84 @@ module API

return flatModelicaCode
end

"""
installPackage(omc, pkg;
version="",
exactMatch=false)
Install package `pkg` with given `version`. If `version=""` try to install
most recent version of package. If `exactMatch` is true install exact
version, even if there are more recent backwards.compatible versions
available.
See [OpenModelica scripting API `installPackage`](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/scripting_api.html#installpackage)
or [Package Management](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/packagemanager.html#using-the-package-manager-from-the-interactive-environment).
"""
function installPackage(omc::OMJulia.OMCSession, pkg::String; version::String="", exactMatch::Bool=false)
success = OMJulia.sendExpression(omc, "installPackage($pkg, version=\"$version\", exactMatch=$exactMatch)")
if !success
throw(OMJulia.API.ScriptingError(omc, msg = "installPackage($pkg, version=$version, exactMatch=$exactMatch)"))
end
return success
end

"""
updatePackageIndex(omc)
Update package index list.
The package manager contacts OSMC sersers and updated the internally sotred
list of available packages.
See [OpenModelica scripting API `updatePackageIndex`](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/scripting_api.html#updatepackageindex)
or [Package Management](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/packagemanager.html#using-the-package-manager-from-the-interactive-environment).
"""
function updatePackageIndex(omc::OMJulia.OMCSession)
success = OMJulia.sendExpression(omc, "updatePackageIndex()")
if !success
throw(OMJulia.API.ScriptingError(omc, msg = "updatePackageIndex()"))
end
return success
end

"""
getAvailablePackageVersions(omc, pkg; version="")
Get available package versions of `pkg`.
Lists all available versions of the Buildings library on the OSMC server,
starting from the most recent one, in descending order of priority. Note
that pre-release versions have lower priority than all other versions.
See [OpenModelica scripting API `getAvailablePackageVersions`](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/scripting_api.html#getavailablepackageversions)
or [Package
Management](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/packagemanager.html#using-the-package-manager-from-the-interactive-environment).
"""
function getAvailablePackageVersions(omc::OMJulia.OMCSession, pkg::String; version::String="")
versions = OMJulia.sendExpression(omc, "getAvailablePackageVersions($pkg, version=\"$version\")")
if length(versions) == 0
errorString = strip(OMJulia.sendExpression(omc, "getErrorString()"))
if errorString != ""
throw(OMJulia.API.ScriptingError(omc, msg = "getAvailablePackageVersions($pkg, version=$version)", errorString=errorString))
end
end
return versions
end

"""
upgradeInstalledPackages(omc; installNewestVersions=true)
Installs the latest available version of all installed packages.
See [OpenModelica scripting API `upgradeInstalledPackages`](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/scripting_api.html#upgradeinstalledpackages)
or [Package
Management](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/packagemanager.html#using-the-package-manager-from-the-interactive-environment).
"""
function upgradeInstalledPackages(omc::OMJulia.OMCSession; installNewestVersions::Bool=true)
success = OMJulia.sendExpression(omc, "upgradeInstalledPackages($installNewestVersions)")
if !success
throw(OMJulia.API.ScriptingError(omc, msg = "upgradeInstalledPackages($installNewestVersions)"))
end
return success
end
end
8 changes: 8 additions & 0 deletions test/apiTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ import OMJulia
end

omc = OMJulia.OMCSession()
# Install packages
@test OMJulia.API.updatePackageIndex(omc)
versions = OMJulia.API.getAvailablePackageVersions(omc, "Modelica", version="3.0.0+maint.om")
@test "3.0.0+maint.om" in versions
@test OMJulia.API.upgradeInstalledPackages(omc)
@test OMJulia.API.installPackage(omc, "Modelica", version = "")

# Load file
@test OMJulia.API.loadFile(omc, joinpath(@__DIR__, "../docs/testmodels/BouncingBall.mo"))

# Enter non-existing directory
Expand Down

2 comments on commit ed8ac12

@AnHeuermann
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/108379

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.2 -m "<description of version>" ed8ac12a6afafc2a741d59c7b48c594a2103f3c6
git push origin v0.3.2

Please sign in to comment.