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

Use semver to pin only major/minor plugin versions #5435

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package nextflow.plugin

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.extension.FilesEx
import org.pf4j.ManifestPluginDescriptorFinder
import org.pf4j.PluginDescriptor
import org.pf4j.update.FileDownloader
import org.pf4j.update.FileVerifier
import org.pf4j.update.PluginInfo
import org.pf4j.update.UpdateRepository
import org.pf4j.update.verifier.CompoundVerifier

import java.nio.file.Path

/**
* Implementation of UpdateRepository which looks in a local directory of already-downloaded
* plugins to find available versions.
*/
@CompileStatic
@Slf4j
class LocalUpdateRepository implements UpdateRepository {
private final String id
private final Path dir
private Map<String, PluginInfo> plugins

LocalUpdateRepository(String id, Path dir) {
this.id = id
this.dir = dir
}

@Override
String getId() {
return id
}

@Override
URL getUrl() {
return dir.toUri().toURL()
}

@Override
Map<String, PluginInfo> getPlugins() {
if( !plugins )
plugins = loadPlugins(dir)
return plugins
}

@Override
PluginInfo getPlugin(String id) {
return getPlugins().get(id)
}

@Override
void refresh() {
this.plugins = null
}

@Override
FileDownloader getFileDownloader() {
// plugins in this repo are already downloaded, so treat any download url as a file path
return (URL url) -> Path.of(url.toURI())
}

@Override
FileVerifier getFileVerifier() {
return new CompoundVerifier()
}

private static Map<String, PluginInfo> loadPlugins(Path dir) {
// each plugin is stored in a dir called $id-$version; grab the descriptor from each
final manifestReader = new ManifestPluginDescriptorFinder()
final descriptors = FilesEx.listFiles(dir)
.collect { plugin -> new LocalPlugin(plugin, manifestReader.find(plugin)) }

// now group the descriptors by id, to create a PluginInfo with list of versions
return descriptors
.groupBy { d -> d.getPluginId() }
.collectEntries { id, versions -> List.of(id, toPluginInfo(id, versions)) }
}

private static PluginInfo toPluginInfo(String id, List<LocalPlugin> versions) {
final info = new PluginInfo()
info.id = id
info.releases = versions.collect { v ->
final release = new PluginInfo.PluginRelease()
release.version = v.version
release.requires = v.requires
release.url = v.path.toUri().toURL()
if( !info.provider && v.provider )
info.provider = v.provider
return release
}
return info
}

private static class LocalPlugin {
Path path

@Delegate
PluginDescriptor descriptor

LocalPlugin(Path path, PluginDescriptor descriptor) {
this.path = path
this.descriptor = descriptor
}
}
}
57 changes: 38 additions & 19 deletions modules/nf-commons/src/main/nextflow/plugin/PluginUpdater.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import dev.failsafe.function.CheckedSupplier
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.BuildInfo
import nextflow.Const
import nextflow.SysEnv
import nextflow.extension.FilesEx
import nextflow.file.FileHelper
import nextflow.file.FileMutex
import org.pf4j.InvalidPluginDescriptorException
import org.pf4j.PluginDependency
import org.pf4j.PluginRuntimeException
import org.pf4j.PluginState
Expand Down Expand Up @@ -67,23 +67,30 @@ class PluginUpdater extends UpdateManager {

private boolean pullOnly

private boolean offline

private DefaultPlugins defaultPlugins = DefaultPlugins.INSTANCE

protected PluginUpdater(CustomPluginManager pluginManager) {
super(pluginManager)
this.pluginManager = pluginManager
}

PluginUpdater(CustomPluginManager pluginManager, Path pluginsRoot, URL repo) {
super(pluginManager, wrap(repo))
PluginUpdater(CustomPluginManager pluginManager, Path pluginsRoot, URL repo, boolean offline) {
super(pluginManager, wrap(repo, pluginsRoot, offline))
this.offline = offline
this.pluginsStore = pluginsRoot
this.pluginManager = pluginManager
}

static private List<UpdateRepository> wrap(URL repo) {
static private List<UpdateRepository> wrap(URL remote, Path local, boolean offline) {
List<UpdateRepository> result = new ArrayList<>(1)
result << new DefaultUpdateRepository('nextflow.io', repo)
result.addAll(customRepos())
if( offline ) {
result.add(new LocalUpdateRepository('downloaded', local))
} else {
result.add(new DefaultUpdateRepository('nextflow.io', remote))
result.addAll(customRepos())
}
return result
}

Expand Down Expand Up @@ -199,19 +206,18 @@ class PluginUpdater extends UpdateManager {
}

private Path download0(String id, String version) {
// 0. check if version is specified
if( !version )
throw new InvalidPluginDescriptorException("Missing version for plugin $id")
log.info "Downloading plugin ${id}@${version}"

// 0. check if already exists
// 1. check if already exists
final pluginPath = pluginsStore.resolve("$id-$version")
if( FilesEx.exists(pluginPath) ) {
return pluginPath
}

// 1. determine the version
if( !version )
version = getLastPluginRelease(id)
log.info "Downloading plugin ${id}@${version}"

// 2. Download to temporary location
// 2. download to temporary location
Path downloaded = safeDownloadPlugin(id, version);

// 3. unzip the content and delete downloaded file
Expand Down Expand Up @@ -314,19 +320,28 @@ class PluginUpdater extends UpdateManager {
new File(tmp, "nextflow-plugin-${id}-${version}.lock")
}

private boolean load0(String id, String version) {
private boolean load0(String id, String requestedVersion) {
assert id, "Missing plugin Id"

if( version == null )
if( offline && !requestedVersion )
throw new IllegalStateException("Cannot find version for $id plugin -- plugin versions MUST be specified in offline mode")

def version = requestedVersion
if( !version )
version = getLastPluginRelease(id)?.version
else if( !Version.isValid(version) )
// a version is 'valid' if it's an exact semver version "major.minor.patch" so
// if it's not that, treat it as a version constraint and look for matches
version = findNewestMatchingRelease(id, version)?.version

final offline = SysEnv.get('NXF_OFFLINE')=='true'
if( !version ) {
final msg = offline
? "Cannot find version for $id plugin -- plugin versions MUST be specified in offline mode"
final msg = requestedVersion
? "Cannot find version of $id plugin matching $requestedVersion"
: "Cannot find latest version of $id plugin"
throw new IllegalStateException(msg)
}
if( version != requestedVersion )
log.debug "Plugin $id version $requestedVersion resolved to: $version"

def pluginPath = pluginsStore.resolve("$id-$version")
if( !FilesEx.exists(pluginPath) ) {
Expand Down Expand Up @@ -405,6 +420,10 @@ class PluginUpdater extends UpdateManager {
log.debug "Update not supported during development mode"
return false
}
if( offline ) {
log.debug "Update not supported in offline mode"
return false
}

if( !version )
version = getLastPluginRelease(pluginId)?.version
Expand Down Expand Up @@ -446,7 +465,7 @@ class PluginUpdater extends UpdateManager {
throw new IllegalArgumentException("Unknown plugin id: $id")

// note: order releases list by descending version numbers ie. latest version comes first
def releases = pluginInfo.releases.sort(false) { a,b -> Version.valueOf(b.version) <=> Version.valueOf(a.version) }
def releases = pluginInfo.releases.sort(false) { a,b -> Version.parse(b.version) <=> Version.parse(a.version) }
for (PluginInfo.PluginRelease rel : releases ) {
if( !versionManager.checkVersionConstraint(rel.version, verConstraint) || !rel.url )
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class PluginsFacade implements PluginStateListener {

private String mode
private Path root
private boolean offline
private PluginUpdater updater
private CustomPluginManager manager
private DefaultPlugins defaultPlugins = DefaultPlugins.INSTANCE
Expand All @@ -55,14 +56,16 @@ class PluginsFacade implements PluginStateListener {
PluginsFacade() {
mode = getPluginsMode()
root = getPluginsDir()
offline = env.get('NXF_OFFLINE') == 'true'
if( mode==DEV_MODE && root.toString()=='plugins' && !isRunningFromDistArchive() )
root = detectPluginsDevRoot()
System.setProperty('pf4j.mode', mode)
}

PluginsFacade(Path root, String mode=PROD_MODE) {
PluginsFacade(Path root, String mode=PROD_MODE, boolean offline=false) {
this.mode = mode
this.root = root
this.offline = offline
System.setProperty('pf4j.mode', mode)
}

Expand Down Expand Up @@ -182,7 +185,7 @@ class PluginsFacade implements PluginStateListener {

protected PluginUpdater createUpdater(Path root, CustomPluginManager manager) {
return ( mode!=DEV_MODE
? new PluginUpdater(manager, root, new URL(indexUrl))
? new PluginUpdater(manager, root, new URL(indexUrl), offline)
: new DevPluginUpdater(manager) )
}

Expand Down
Loading
Loading