Skip to content

Commit 3bed458

Browse files
arajkumarjeffmaury
andauthored
fix: Associate LSP bundle download with app preload instead of project (#36)
* refactor: Associate LSP bundle download with app preload instead of project Signed-off-by: Arunprasad Rajkumar <[email protected]> Signed-off-by: Jeff MAURY <[email protected]> Co-authored-by: Jeff MAURY <[email protected]>
1 parent d65259b commit 3bed458

File tree

11 files changed

+205
-181
lines changed

11 files changed

+205
-181
lines changed

src/main/java/org/jboss/tools/intellij/analytics/AnalyticsPersistentSettings.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jboss.tools.intellij.analytics;
2+
3+
import org.kohsuke.github.GitHub;
4+
import org.kohsuke.github.GHAsset;
5+
import org.kohsuke.github.GHRepository;
6+
import org.kohsuke.github.GHRelease;
7+
import java.io.IOException;
8+
9+
public class GitHubRelease {
10+
private final GHRepository repo;
11+
12+
public GitHubRelease(final String repository) throws IOException {
13+
final GitHub github = GitHub.connectAnonymously();
14+
this.repo = github.getRepository(repository);
15+
}
16+
17+
public String getLatestRelease() throws IOException {
18+
return this.repo.getLatestRelease().getTagName();
19+
}
20+
21+
public String getDownloadUri(final String releaseLabel, final String fileLabel) throws IOException {
22+
final GHRelease release = this.repo.getReleaseByTagName(releaseLabel);
23+
final GHAsset asset = release.listAssets()
24+
.toList()
25+
.stream()
26+
.filter(a -> a.getLabel().equals(fileLabel))
27+
.findFirst()
28+
.orElseThrow(() -> new IOException(fileLabel + ": unable to download"));
29+
return asset.getBrowserDownloadUrl();
30+
}
31+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.jboss.tools.intellij.analytics;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import com.intellij.ide.plugins.IdeaPluginDescriptor;
7+
import com.intellij.ide.plugins.PluginManager;
8+
import com.intellij.openapi.application.ApplicationManager;
9+
import com.intellij.openapi.components.Service;
10+
import com.intellij.openapi.components.ServiceManager;
11+
import com.intellij.openapi.diagnostic.Logger;
12+
import com.intellij.openapi.extensions.PluginId;
13+
import com.intellij.openapi.progress.ProgressIndicator;
14+
import com.intellij.openapi.project.Project;
15+
import com.intellij.util.io.HttpRequests;
16+
17+
public final class GitHubReleaseDownloader {
18+
private final IdeaPluginDescriptor descriptor = PluginManager.getPlugin(PluginId.getId("org.jboss.tools.intellij.analytics"));
19+
private final String fileName;
20+
private final ICookie cookies;
21+
private final GitHubRelease release;
22+
23+
public GitHubReleaseDownloader(final String fileName, final ICookie cookies) throws IOException {
24+
this.fileName = fileName;
25+
this.cookies = cookies;
26+
this.release = new GitHubRelease("fabric8-analytics/fabric8-analytics-lsp-server");
27+
}
28+
29+
private boolean isNewRelease(final String releaseLabel) {
30+
final String currentVersion = cookies.getValue(ICookie.Name.LSPVersion);
31+
return !releaseLabel.equals(currentVersion);
32+
}
33+
34+
public File download(final ProgressIndicator indicator) throws IOException {
35+
final File dest = new File(descriptor.getPath(), fileName);
36+
final String latestReleaseTag = this.release.getLatestRelease();
37+
if (!isNewRelease(latestReleaseTag) && dest.exists()) {
38+
return dest;
39+
}
40+
final String url = this.release.getDownloadUri(latestReleaseTag, this.fileName);
41+
HttpRequests
42+
.request(url)
43+
.productNameAsUserAgent()
44+
.saveToFile(dest, indicator);
45+
46+
dest.setExecutable(true);
47+
cookies.setValue(ICookie.Name.LSPVersion, latestReleaseTag);
48+
return dest;
49+
}
50+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.jboss.tools.intellij.analytics;
2+
3+
public interface ICookie {
4+
enum Name {
5+
LSPVersion,
6+
}
7+
8+
void setValue(Name name, String value);
9+
String getValue(Name name);
10+
}

src/main/java/org/jboss/tools/intellij/analytics/LSPBundle.java

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/main/java/org/jboss/tools/intellij/analytics/LSPBundleException.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/org/jboss/tools/intellij/analytics/PostStartupActivity.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.jboss.tools.intellij.analytics;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import com.intellij.openapi.application.ApplicationManager;
6+
import com.intellij.openapi.application.PreloadingActivity;
7+
import com.intellij.openapi.progress.ProgressIndicator;
8+
import com.intellij.openapi.diagnostic.Logger;
9+
import com.intellij.openapi.components.ServiceManager;
10+
import org.wso2.lsp4intellij.IntellijLanguageClient;
11+
12+
public final class PreloadLanguageServer extends PreloadingActivity {
13+
private static final Logger log = Logger.getInstance(PreloadLanguageServer.class);
14+
private final ICookie cookies = ServiceManager.getService(Settings.class);
15+
16+
private void attachLanguageClient(final File cliFile) {
17+
final String[] EXTENSIONS = {"xml", "json", "txt"};
18+
final String[] cmds = {cliFile.toString(), "--stdio"};
19+
ApplicationManager.getApplication().invokeAndWait(() -> {
20+
for (String ext : EXTENSIONS) {
21+
AnalyticsLanguageServerDefinition serverDefinition = new AnalyticsLanguageServerDefinition(ext, cmds);
22+
IntellijLanguageClient.addServerDefinition(serverDefinition);
23+
IntellijLanguageClient.addExtensionManager(ext, serverDefinition);
24+
}
25+
});
26+
log.warn(String.format("lsp registration done %s", cliFile));
27+
}
28+
29+
@Override
30+
public void preload(ProgressIndicator indicator) {
31+
if (ApplicationManager.getApplication().isUnitTestMode()) {
32+
return;
33+
}
34+
log.debug("lsp preload called");
35+
try {
36+
final String devUrl = System.getenv("ANALYTICS_LSP_FILE_PATH");
37+
File lspBundle;
38+
if (devUrl != null) {
39+
lspBundle = new File(devUrl);
40+
} else {
41+
final GitHubReleaseDownloader bundle = new GitHubReleaseDownloader(Platform.current.lspBundleName, cookies);
42+
lspBundle = bundle.download(indicator);
43+
}
44+
attachLanguageClient(lspBundle);
45+
} catch(IOException ex) {
46+
log.warn("lsp download fail", ex);
47+
}
48+
}
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.jboss.tools.intellij.analytics;
2+
3+
import com.intellij.openapi.components.PersistentStateComponent;
4+
import com.intellij.openapi.components.RoamingType;
5+
import com.intellij.openapi.components.State;
6+
import com.intellij.openapi.components.Service;
7+
import com.intellij.openapi.components.Storage;
8+
import com.intellij.util.xmlb.XmlSerializerUtil;
9+
import com.intellij.util.xmlb.annotations.MapAnnotation;
10+
11+
import java.util.Map;
12+
import java.util.HashMap;
13+
14+
@Service
15+
@State(
16+
name = "Settings",
17+
storages = {
18+
@Storage(file = "analytics.settings.xml", roamingType = RoamingType.DISABLED)
19+
})
20+
public final class Settings implements ICookie, PersistentStateComponent<Settings> {
21+
22+
// str representation of ICookie.Name values are key.
23+
private Map<String, String> settings = new HashMap();
24+
25+
@Override
26+
public Settings getState() {
27+
return this;
28+
}
29+
30+
@Override
31+
public void loadState(Settings state) {
32+
XmlSerializerUtil.copyBean(state, this);
33+
}
34+
35+
@Override
36+
public void setValue(ICookie.Name name, String value) {
37+
this.settings.put(name.name(), value);
38+
}
39+
40+
@Override
41+
public String getValue(ICookie.Name name) {
42+
return this.settings.getOrDefault(name.name(), "");
43+
}
44+
45+
@MapAnnotation
46+
public Map<String, String> getSettings() {
47+
return settings;
48+
}
49+
50+
public void setSettings(Map<String, String> settings) {
51+
this.settings = settings;
52+
}
53+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
<externalAnnotator id="LSPAnnotator-xml" language="XML" implementationClass="org.wso2.lsp4intellij.contributors.annotator.LSPAnnotator"/>
9494
<externalAnnotator id="LSPAnnotator-json" language="JSON" implementationClass="org.wso2.lsp4intellij.contributors.annotator.LSPAnnotator"/>
9595
<externalAnnotator id="LSPAnnotator-txt" language="TEXT" implementationClass="org.wso2.lsp4intellij.contributors.annotator.LSPAnnotator"/>
96-
<postStartupActivity implementation="org.jboss.tools.intellij.analytics.PostStartupActivity"/>
96+
<preloadingActivity implementation="org.jboss.tools.intellij.analytics.PreloadLanguageServer"
97+
id="org.jboss.tools.intellij.analytics.PostStartupActivity"/>
9798
</extensions>
9899

99100
<application-components>

0 commit comments

Comments
 (0)