Skip to content

Commit

Permalink
Patch level select with @~ (#338)
Browse files Browse the repository at this point in the history
Also changed some error messages to be more informative
Co-authored-by: Joachim Krech <8290187+jkrech@users.noreply.github.com>
  • Loading branch information
bgn42 authored Sep 2, 2024
1 parent 2ed47fc commit f4ac119
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 44 deletions.
16 changes: 8 additions & 8 deletions cmd/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ Add a pack using the following "<pack>" specification or using packs provided by
$ cpackget add path/to/Vendor.Pack.pdsc
Use this syntax if you are installing a pack that has not
been released yet. This will install it as a local pack and
keep a reference in ".Local/local_repository.pidx".
been released yet. This will add a reference in ".Local/local_repository.pidx".
To select a specific version use: Vendor::Pack@x.y.z
To select the newest version of a major version use: Vendor::Pack@^x.y.z
To select any newer version use: Vendor::Pack@>=x.y.z
To install a specific version use: Vendor::Pack@x.y.z
To install the newest version of the major version if a version greater equal x.y.z is not already installed use: Vendor::Pack@^x.y.z
To install the newest version of the major and the minor version if a version greater or equal x.y.z is not already installed use: Vendor::Pack@~x.y.z
To install the newest available version if a version greater or equal x.y.z is not already installed use: Vendor::Pack@>=x.y.z
The file can be a local file or a file hosted somewhere else on the Internet.
If it's hosted somewhere, cpackget will first download it then extract all pack files into "CMSIS_PACK_ROOT/<vendor>/<packName>/<x.y.z>/"
If "-f" is used, cpackget will call "cpackget pack add" on each URL specified in the <packs list> file.`,
The file can be a local file or a file hosted somewhere else on the Internet.
If it's hosted somewhere, cpackget will first download it then extract all pack files into "CMSIS_PACK_ROOT/<vendor>/<packName>/<x.y.z>/"
If "-f" is used, cpackget will call "cpackget pack add" on each URL specified in the <packs list> file.`,
Args: cobra.MinimumNArgs(0),
PersistentPreRunE: configureInstaller,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
28 changes: 25 additions & 3 deletions cmd/installer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (p *PackType) extractEula(packPath string) error {
return os.WriteFile(eulaFileName, eulaContents, utils.FileModeRO)
}

// resolveVersionModifier takes into account eventual versionModifiers (@, @^ and @>=) to determine
// resolveVersionModifier takes into account eventual versionModifiers (@, @^, @~ and @>=) to determine
// which version of a pack should be targeted for installation
func (p *PackType) resolveVersionModifier(pdscXML *xml.PdscXML) {
log.Debugf("Resolving version modifier for \"%s\" using PDSC \"%s\"", p.path, pdscXML.FileName)
Expand Down Expand Up @@ -531,7 +531,29 @@ func (p *PackType) resolveVersionModifier(pdscXML *xml.PdscXML) {
p.targetVersion = pdscXML.LatestVersion()
log.Debugf("- resolved (@^) as %s", p.targetVersion)
} else {
log.Errorf("Tried to install major version %s.x.x, highest available major version is %s.x.x", p.Version[:1], pdscXML.LatestVersion()[:1])
log.Errorf("No compatible minor version available for pack version >= %s, highest available major version is %s.x.x", p.Version, utils.SemverMajor(pdscXML.LatestVersion()))
}
return
}

// The next tricky one is @~, because it needs to be the latest
// release matching the major and minor number.
// The releases in the PDSC file are sorted from latest to oldest
if p.versionModifier == utils.PatchVersion {
for _, version := range pdscXML.AllReleases() {
sameMajorMinor := utils.SemverMajorMinor(version) == utils.SemverMajorMinor(p.Version)
if sameMajorMinor && utils.SemverCompare(version, p.Version) >= 0 {
p.targetVersion = version
log.Debugf("- resolved (@~) as %s", p.targetVersion)
return
}
}
// Check if at least same Major.Minor version exists
if utils.SemverCompare(p.targetVersion, p.Version) > 0 {
p.targetVersion = pdscXML.LatestVersion()
log.Debugf("- resolved (@~) as %s", p.targetVersion)
} else {
log.Errorf("No compatible patch version available for pack version >= %s, highest available major.minor version is %s.x", p.Version, utils.SemverMajorMinor(pdscXML.LatestVersion()))
}
return
}
Expand Down Expand Up @@ -638,7 +660,7 @@ func (p *PackType) PdscFileNameWithVersion() string {
}

// GetVersion makes sure to get the latest version for the pack
// after parsing possible version modifiers (@^, @>=)
// after parsing possible version modifiers (@^, @~, @>=)
func (p *PackType) GetVersion() string {
if p.versionModifier != utils.ExactVersion {
return p.targetVersion
Expand Down
25 changes: 25 additions & 0 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,11 @@ func FindPackURL(pack *PackType) (string, error) {
return "", errs.ErrPackVersionNotAvailable
}
}
if pack.versionModifier == utils.PatchVersion {
if semver.MajorMinor("v"+releaseTag.Version) != semver.MajorMinor("v"+pack.Version) {
return "", errs.ErrPackVersionNotAvailable
}
}

if releaseTag == nil {
return "", errs.ErrPackVersionNotFoundInPdsc
Expand Down Expand Up @@ -892,6 +897,11 @@ func FindPackURL(pack *PackType) (string, error) {
return "", errs.ErrPackVersionNotAvailable
}
}
if pack.versionModifier == utils.PatchVersion {
if semver.MajorMinor("v"+releaseTag.Version) != semver.MajorMinor("v"+pack.Version) {
return "", errs.ErrPackVersionNotAvailable
}
}

if releaseTag == nil {
return "", errs.ErrPackVersionNotFoundInPdsc
Expand Down Expand Up @@ -1104,6 +1114,21 @@ func (p *PacksInstallationType) PackIsInstalled(pack *PackType) bool {
return false
}

// Check if there is a greater version with same Major and Minor number
if pack.versionModifier == utils.PatchVersion {
log.Debugf("Checking for installed packs @~%s", pack.Version)
for _, version := range installedVersions {
log.Debugf("- checking against: %s", version)
sameMajorMinor := semver.MajorMinor("v"+version) == semver.MajorMinor("v"+pack.Version)
if sameMajorMinor && utils.SemverCompare(version, pack.Version) >= 0 {
pack.targetVersion = version
return true
}
}

return false
}

if pack.versionModifier == utils.RangeVersion {
for _, version := range installedVersions {
if utils.SemverCompareRange(version, pack.Version) == 0 {
Expand Down
Loading

0 comments on commit f4ac119

Please sign in to comment.