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

Хелпер для загрузки BSL LS #3

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import me.qoomon.gradle.gitversioning.GitVersioningPluginConfig
import me.qoomon.gradle.gitversioning.GitVersioningPluginConfig.CommitVersionDescription
import me.qoomon.gradle.gitversioning.GitVersioningPluginConfig.VersionDescription
import java.util.Calendar
import java.net.URI

plugins {
java
Expand All @@ -18,6 +19,7 @@ group = "com.github.1c-syntax"

repositories {
mavenCentral()
maven { url = URI("https://jitpack.io") }
}

val junitVersion = "5.6.0"
Expand All @@ -26,6 +28,13 @@ dependencies {

compileOnly("org.projectlombok", "lombok", lombok.version)

implementation("com.github.hub4j:github-api:github-api-1.114")
implementation("org.slf4j", "slf4j-api", "1.8.0-beta4")
implementation("org.slf4j", "slf4j-simple", "1.8.0-beta4")

// https://mvnrepository.com/artifact/commons-io/commons-io
implementation("commons-io", "commons-io", "2.7")

testImplementation("org.junit.jupiter", "junit-jupiter-api", junitVersion)
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", junitVersion)

Expand Down
107 changes: 107 additions & 0 deletions src/main/java/com/github/_1c_syntax/utils/DownloadHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* This file is a part of 1c-syntax utils.
*
* Copyright © 2018-2020
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* 1c-syntax utils is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* 1c-syntax utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with 1c-syntax utils.
*/
package com.github._1c_syntax.utils;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.kohsuke.github.GHAsset;
import org.kohsuke.github.GHRelease;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Optional;

@UtilityClass
@Slf4j
public class DownloadHelper {

private final String REPO_PATH = "1c-syntax/bsl-language-server";
private final String CONTENT_TYPE = "application/zip";
private final int CONNECTION_TIMEOUT = 900;

public final String SUPPLY_FOR_WIN = "win";
public final String SUPPLY_FOR_MAC = "mac";
public final String SUPPLY_FOR_LINUX = "nix";

public Optional<GHAsset> getLatestRelease(String token, String supplyVariant, Path pathForSave) {
return getLatestRelease(token, supplyVariant, pathForSave, false);
}

public Optional<GHAsset> getLatestRelease(String token, String supplyVariant, Path pathForSave, boolean prerelease) {
Optional<GHAsset> ghAsset;
try {
GitHub github = GitHub.connectUsingOAuth(token);
ghAsset = Optional.ofNullable(github.getRepository(REPO_PATH))
.flatMap(repo -> {
try {
return getAssetsFromRelease(repo, supplyVariant, prerelease);
} catch (IOException e) {
LOGGER.error("Error finding asset from release", e);
return Optional.empty();
}
});

} catch (IOException e) {
LOGGER.error("Error finding asset from release", e);
ghAsset = Optional.empty();
}
if (ghAsset.isPresent() && !downloadFromAsset(ghAsset.get(), pathForSave)) {
return Optional.empty();
}
return ghAsset;
}

private boolean downloadFromAsset(GHAsset ghAsset, Path path) {
try {
var file = Path.of(path.toString(), ghAsset.getName()).toFile();
FileUtils.copyURLToFile(new URL(ghAsset.getBrowserDownloadUrl()), file, CONNECTION_TIMEOUT, 0);
} catch (IOException e) {
LOGGER.error("An error occurred while loading", e);
return false;
}
return true;
}

private Optional<GHAsset> getAssetsFromRelease(GHRepository repo, String supplyVariant, boolean prerelease) throws IOException {
var endsName = supplyVariant + ".zip";
var release = getLatestRelease(repo, prerelease);
if (release.isPresent()) {
return release.get().getAssets().stream()
.filter(ghAsset -> ghAsset.getContentType().equals(CONTENT_TYPE)
&& ghAsset.getName().endsWith(endsName))
.findAny();
}
return Optional.empty();
}

private Optional<GHRelease> getLatestRelease(GHRepository repo, boolean prerelease) throws IOException {
Copy link
Member

@nixel2007 nixel2007 Jul 4, 2020

Choose a reason for hiding this comment

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

вот эта логика не совсем верная. если ты посмотришь в реализацию загрузчика для vsc, то в случае если запрашивается последний релиз по каналу prerelease и если релиз вышел после пререлиза, то вернется релиз. а в твоем коде - пререлиз.

Copy link
Member Author

Choose a reason for hiding this comment

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

Спасибо! Посмотрю

return repo.listReleases().toList().stream()
.filter(ghRelease -> ghRelease.isPrerelease() == prerelease)
.limit(1)
.findAny();
}

}
62 changes: 62 additions & 0 deletions src/test/java/com/github/_1c_syntax/utils/DownloadHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This file is a part of 1c-syntax utils.
*
* Copyright © 2018-2020
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* 1c-syntax utils is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* 1c-syntax utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with 1c-syntax utils.
*/
package com.github._1c_syntax.utils;

import org.junit.jupiter.api.Test;
import org.kohsuke.github.GHAsset;

import java.nio.file.Path;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

public class DownloadHelperTest {

private final Path BASE_PATH = Path.of("build", "fixture");

@Test
public void test_getLatestRelease() {
prepareFolder(BASE_PATH);
var token = System.getenv("GITHUB_TOKEN");

var ghAsset = DownloadHelper.getLatestRelease(token, DownloadHelper.SUPPLY_FOR_WIN, BASE_PATH);
checkGHAsset(ghAsset);

ghAsset = DownloadHelper.getLatestRelease(token, DownloadHelper.SUPPLY_FOR_LINUX, BASE_PATH);
checkGHAsset(ghAsset);

ghAsset = DownloadHelper.getLatestRelease(token, DownloadHelper.SUPPLY_FOR_MAC, BASE_PATH, true);
checkGHAsset(ghAsset);
}

void checkGHAsset(Optional<GHAsset> ghAsset) {
assertThat(ghAsset).isPresent();
var file = Path.of(BASE_PATH.toString(), ghAsset.get().getName()).toFile();
assertThat(file).exists();
}


private void prepareFolder(Path path) {
path.toFile().mkdir();
}

}