github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/internal/github/semver.go (about)

     1  package github
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Masterminds/semver"
     7  )
     8  
     9  // SemanticVersion is a struct containing a semantic version
    10  type SemanticVersion struct {
    11  	Version *semver.Version
    12  }
    13  
    14  // NewSemanticVersion creates a new SemanticVersion object with the given version string
    15  func NewSemanticVersion(version string) (*SemanticVersion, error) {
    16  	semverVersion, err := semver.NewVersion(version)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	return &SemanticVersion{
    21  		Version: semverVersion,
    22  	}, nil
    23  }
    24  
    25  // IsRelease returns true if it's a release version
    26  func (s *SemanticVersion) IsRelease() bool {
    27  	// Limit to v2
    28  	if s.Version.Major() != 2 {
    29  		return false
    30  	}
    31  	return len(s.Version.Prerelease()) == 0 && len(s.Version.Metadata()) == 0
    32  }
    33  
    34  // IsPreRelease returns true if it's a prerelease version
    35  func (s *SemanticVersion) IsPreRelease() bool {
    36  	// Limit to v1
    37  	if s.Version.Major() != 2 {
    38  		return false
    39  	}
    40  	return len(s.Version.Prerelease()) > 0
    41  }
    42  
    43  func (s *SemanticVersion) String() string {
    44  	return s.Version.String()
    45  }
    46  
    47  // IsGreaterThan returns true if this version is greater than the given version
    48  func (s *SemanticVersion) IsGreaterThan(version *SemanticVersion) (bool, error) {
    49  	// Set up new constraint
    50  	constraint, err := semver.NewConstraint("> " + version.Version.String())
    51  	if err != nil {
    52  		return false, err
    53  	}
    54  
    55  	// Check if the desired one is greater than the requested on
    56  	success, msgs := constraint.Validate(s.Version)
    57  	if !success {
    58  		return false, msgs[0]
    59  	}
    60  	return true, nil
    61  }
    62  
    63  // IsGreaterThanOrEqual returns true if this version is greater than or equal the given version
    64  func (s *SemanticVersion) IsGreaterThanOrEqual(version *SemanticVersion) (bool, error) {
    65  	// Set up new constraint
    66  	constraint, err := semver.NewConstraint(">= " + version.Version.String())
    67  	if err != nil {
    68  		return false, err
    69  	}
    70  
    71  	// Check if the desired one is greater than the requested on
    72  	success, msgs := constraint.Validate(s.Version)
    73  	if !success {
    74  		return false, msgs[0]
    75  	}
    76  	return true, nil
    77  }
    78  
    79  // MainVersion returns the main version of any version+prerelease+metadata
    80  // EG: MainVersion("1.2.3-pre") => "1.2.3"
    81  func (s *SemanticVersion) MainVersion() *SemanticVersion {
    82  	mainVersion := fmt.Sprintf("%d.%d.%d", s.Version.Major(), s.Version.Minor(), s.Version.Patch())
    83  	result, _ := NewSemanticVersion(mainVersion)
    84  	return result
    85  }
    86  
    87  // SemverCollection is a collection of SemanticVersion objects
    88  type SemverCollection []*SemanticVersion
    89  
    90  // Len returns the length of a collection. The number of Version instances
    91  // on the slice.
    92  func (c SemverCollection) Len() int {
    93  	return len(c)
    94  }
    95  
    96  // Less is needed for the sort interface to compare two Version objects on the
    97  // slice. If checks if one is less than the other.
    98  func (c SemverCollection) Less(i, j int) bool {
    99  	return c[i].Version.LessThan(c[j].Version)
   100  }
   101  
   102  // Swap is needed for the sort interface to replace the Version objects
   103  // at two different positions in the slice.
   104  func (c SemverCollection) Swap(i, j int) {
   105  	c[i], c[j] = c[j], c[i]
   106  }