|
| 1 | +/* |
| 2 | + * Copyright 2026 Flamingock (https://www.flamingock.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.flamingock.cli.executor.skills; |
| 17 | + |
| 18 | +import io.flamingock.cli.executor.util.archive.ZipArchiveExtractor; |
| 19 | +import io.flamingock.cli.executor.util.filesystem.DirectoryLister; |
| 20 | +import io.flamingock.cli.executor.util.filesystem.DirectoryReplacer; |
| 21 | +import io.flamingock.cli.executor.util.filesystem.FileSystemUtils; |
| 22 | +import io.flamingock.cli.executor.util.filesystem.TemporaryDirectoryFactory; |
| 23 | +import io.flamingock.cli.executor.util.http.HttpFileDownloader; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.URI; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.function.Consumer; |
| 32 | +import java.util.function.Supplier; |
| 33 | + |
| 34 | +/** |
| 35 | + * Executes the shared staged pipeline that installs official Flamingock skills. |
| 36 | + */ |
| 37 | +public class SkillsInstallationPipeline { |
| 38 | + |
| 39 | + private static final URI OFFICIAL_SKILLS_ARCHIVE_URI = |
| 40 | + URI.create("https://github.com/flamingock/flamingock-skills/archive/refs/heads/release.zip"); |
| 41 | + private static final String ARCHIVE_FILE_NAME = "flamingock-skills.zip"; |
| 42 | + private static final String EXTRACTION_DIRECTORY_NAME = "extracted"; |
| 43 | + private static final String SKILL_DIRECTORY_PREFIX = "flamingock-"; |
| 44 | + private static final String TEMP_DIRECTORY_PREFIX = "flamingock-skills-"; |
| 45 | + private static final String DOWNLOAD_LABEL = "official Flamingock skills"; |
| 46 | + private static final String ARCHIVE_DESCRIPTION = "skills archive"; |
| 47 | + private static final String USER_AGENT = "Flamingock CLI/1.0 install-skills"; |
| 48 | + |
| 49 | + private final HttpFileDownloader downloader; |
| 50 | + private final ZipArchiveExtractor extractor; |
| 51 | + private final DirectoryLister directoryLister; |
| 52 | + private final DirectoryReplacer replacer; |
| 53 | + private final Supplier<Path> workspaceSupplier; |
| 54 | + private final Consumer<Path> cleanup; |
| 55 | + |
| 56 | + public SkillsInstallationPipeline() { |
| 57 | + this(new HttpFileDownloader(), |
| 58 | + new ZipArchiveExtractor(), |
| 59 | + new DirectoryLister(), |
| 60 | + new DirectoryReplacer(), |
| 61 | + () -> TemporaryDirectoryFactory.create(TEMP_DIRECTORY_PREFIX, "skill installation"), |
| 62 | + FileSystemUtils::deleteRecursively); |
| 63 | + } |
| 64 | + |
| 65 | + SkillsInstallationPipeline( |
| 66 | + HttpFileDownloader downloader, |
| 67 | + ZipArchiveExtractor extractor, |
| 68 | + DirectoryLister directoryLister, |
| 69 | + DirectoryReplacer replacer, |
| 70 | + Supplier<Path> workspaceSupplier, |
| 71 | + Consumer<Path> cleanup |
| 72 | + ) { |
| 73 | + this.downloader = downloader; |
| 74 | + this.extractor = extractor; |
| 75 | + this.directoryLister = directoryLister; |
| 76 | + this.replacer = replacer; |
| 77 | + this.workspaceSupplier = workspaceSupplier; |
| 78 | + this.cleanup = cleanup; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Runs the download, extract, enumerate, replace, and cleanup stages. |
| 83 | + * |
| 84 | + * @param targets resolved installation targets |
| 85 | + * @return installation result summary |
| 86 | + */ |
| 87 | + public SkillsInstallationResult install(List<SkillsInstallationTarget> targets) { |
| 88 | + Objects.requireNonNull(targets, "targets must not be null"); |
| 89 | + if (targets.isEmpty()) { |
| 90 | + throw new IllegalStateException("No skills installation targets were resolved. Choose a destination and retry."); |
| 91 | + } |
| 92 | + |
| 93 | + Path workspace = workspaceSupplier.get(); |
| 94 | + RuntimeException installationFailure = null; |
| 95 | + try { |
| 96 | + Path archive = downloader.downloadTo( |
| 97 | + workspace, |
| 98 | + OFFICIAL_SKILLS_ARCHIVE_URI, |
| 99 | + ARCHIVE_FILE_NAME, |
| 100 | + USER_AGENT, |
| 101 | + DOWNLOAD_LABEL |
| 102 | + ); |
| 103 | + Path snapshotRoot = extractor.extractSingleRootDirectory( |
| 104 | + archive, |
| 105 | + workspace, |
| 106 | + EXTRACTION_DIRECTORY_NAME, |
| 107 | + ARCHIVE_DESCRIPTION |
| 108 | + ); |
| 109 | + List<Path> skillDirectories = directoryLister.listDirectories( |
| 110 | + snapshotRoot, |
| 111 | + path -> path.getFileName().toString().startsWith(SKILL_DIRECTORY_PREFIX) |
| 112 | + ); |
| 113 | + List<String> installedSkills = new ArrayList<>(); |
| 114 | + for (Path skillDirectory : skillDirectories) { |
| 115 | + installedSkills.add(skillDirectory.getFileName().toString()); |
| 116 | + } |
| 117 | + for (SkillsInstallationTarget target : targets) { |
| 118 | + for (Path skillDirectory : skillDirectories) { |
| 119 | + Path destinationSkillDirectory = target.destinationSkillsDir().resolve(skillDirectory.getFileName().toString()); |
| 120 | + replacer.replaceDirectory(skillDirectory, destinationSkillDirectory); |
| 121 | + } |
| 122 | + } |
| 123 | + return new SkillsInstallationResult(targets, installedSkills); |
| 124 | + } catch (IOException e) { |
| 125 | + installationFailure = new IllegalStateException("Failed to install Flamingock skills: " + e.getMessage(), e); |
| 126 | + throw installationFailure; |
| 127 | + } finally { |
| 128 | + try { |
| 129 | + cleanup.accept(workspace); |
| 130 | + } catch (RuntimeException cleanupFailure) { |
| 131 | + if (installationFailure != null) { |
| 132 | + installationFailure.addSuppressed(cleanupFailure); |
| 133 | + } else { |
| 134 | + throw cleanupFailure; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments