use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class ScriptPlugin method downloadDependenciesBasedOnArtifact.
@Override
public GeneratedArtifacts downloadDependenciesBasedOnArtifact(QName artifactTemplate, IRepository repository) {
ArtifactTemplateId originalId = new ArtifactTemplateId(artifactTemplate);
QName selfContainedVersion = VersionSupport.getSelfContainedVersion(originalId);
ArtifactTemplateId selfContainedId = new ArtifactTemplateId(selfContainedVersion);
if (!repository.exists(selfContainedId)) {
try {
repository.duplicate(originalId, selfContainedId);
} catch (IOException e) {
logger.error("Could not create self-containd artifact template {}", selfContainedId, e);
}
}
ArtifactTemplateFilesDirectoryId originalFilesId = new ArtifactTemplateFilesDirectoryId(selfContainedId);
GeneratedArtifacts generatedArtifacts = new GeneratedArtifacts(artifactTemplate);
generatedArtifacts.selfContainedArtifactQName = selfContainedVersion;
boolean createdSelfContainedVersion = false;
for (RepositoryFileReference containedFile : repository.getContainedFiles(originalFilesId)) {
if (containedFile.getFileName().endsWith(".sh")) {
StringBuilder newScriptContents = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(repository.ref2AbsolutePath(containedFile).toFile()))) {
String line;
ArrayList<String> packageNames = new ArrayList<>();
int packageNameCount = 0;
while ((line = reader.readLine()) != null) {
List<String> strings = Arrays.asList(line.replaceAll("[;&]", "").split("\\s+"));
Iterator<String> words = strings.iterator();
if (words.hasNext() && StringUtils.isNotBlank(line) && line.contains("apt")) {
String word = words.next();
while ("sudo".equals(word) || word.startsWith("-")) {
word = words.next();
}
if (words.hasNext() && ("apt-get".equals(word) || "apt".equals(word))) {
word = words.next();
while (word.startsWith("-")) {
word = words.next();
}
if (word.equals("install") && words.hasNext()) {
words.forEachRemaining(packageToInstall -> {
if (!packageToInstall.startsWith("-")) {
packageNames.add(createDeploymentArtifact(originalId, repository, generatedArtifacts, packageToInstall));
}
});
}
}
}
if (!packageNames.isEmpty() && packageNameCount++ < packageNames.size()) {
createdSelfContainedVersion = true;
packageNames.forEach(packet -> newScriptContents.append(this.updateScriptFile(packet)));
} else {
newScriptContents.append(line).append("\n");
}
}
} catch (IOException e) {
logger.error("Error while reading script file {}", repository.ref2AbsolutePath(containedFile), e);
}
if (newScriptContents.length() > 0) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(repository.ref2AbsolutePath(containedFile).toFile()))) {
writer.write(newScriptContents.toString());
writer.flush();
} catch (IOException e) {
logger.error("Error while writing to script file {}", repository.ref2AbsolutePath(containedFile), e);
}
}
}
}
if (createdSelfContainedVersion) {
return generatedArtifacts;
}
try {
repository.forceDelete(selfContainedId);
} catch (IOException e) {
logger.error("Could not delete not required self-contained {}!", selfContainedId, e);
}
return null;
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class AbstractFileBasedRepository method duplicate.
protected void duplicate(DefinitionsChildId oldId, DefinitionsChildId newId, boolean moveOnly) throws IOException {
Objects.requireNonNull(oldId);
Objects.requireNonNull(newId);
if (oldId.equals(newId)) {
// we do not do anything - even not throwing an error
return;
}
TDefinitions definitions = this.getDefinitions(oldId);
RepositoryFileReference oldRef = BackendUtils.getRefOfDefinitions(oldId);
RepositoryFileReference newRef = BackendUtils.getRefOfDefinitions(newId);
// oldRef points to the definitions file,
// getParent() returns the directory
// we need toFile(), because we rely on FileUtils.moveDirectoryToDirectory
File oldDir = this.id2AbsolutePath(oldRef.getParent()).toFile();
File newDir = this.id2AbsolutePath(newRef.getParent()).toFile();
if (moveOnly) {
org.apache.commons.io.FileUtils.moveDirectory(oldDir, newDir);
} else {
org.apache.commons.io.FileUtils.copyDirectory(oldDir, newDir);
}
// Update definitions and store it
// This also updates the definitions of componentInstanceResource
BackendUtils.updateWrapperDefinitions(newId, definitions, this);
// This works, because the definitions object here is the same as the definitions object treated at copyIdToFields
// newId has to be passed, because the id is final at AbstractComponentInstanceResource
BackendUtils.copyIdToFields((HasIdInIdOrNameField) definitions.getElement(), newId);
try {
BackendUtils.persist(this, newId, definitions);
} catch (InvalidPathException e) {
LOGGER.debug("Invalid path during write", e);
// QUICK FIX
// Somewhere, the first letter is deleted --> /odetypes/http%3A%2F%2Fwww.example.org%2F05/
// We just ignore it for now
}
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class AbstractFileBasedRepository method getNamespaceManager.
@Override
public NamespaceManager getNamespaceManager() {
NamespaceManager manager;
RepositoryFileReference ref = BackendUtils.getRefOfJsonConfiguration(new NamespacesId());
manager = new JsonBasedNamespaceManager(ref2AbsolutePath(ref).toFile(), isLocal);
Configuration configuration = this.getConfiguration(new NamespacesId());
if (!configuration.isEmpty()) {
ConfigurationBasedNamespaceManager old = new ConfigurationBasedNamespaceManager(configuration);
manager.replaceAll(old.getAllNamespaces());
try {
forceDelete(BackendUtils.getRefOfConfiguration(new NamespacesId()));
} catch (IOException e) {
LOGGER.error("Could not remove old namespace configuration.", e);
}
}
return manager;
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class WriterUtils method storeTypes.
public static void storeTypes(IRepository repository, Path path, String namespace, String id) {
LOGGER.debug("Store type: {}", id);
try {
MediaType mediaType = MediaTypes.MEDIATYPE_XSD;
TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
builder.setNamespace(namespace);
builder.setLocation(id + ".xsd");
GenericImportId rid = new XSDImportId(namespace, id, false);
TDefinitions definitions = BackendUtils.createWrapperDefinitions(rid, repository);
definitions.getImport().add(builder.build());
CsarImporter.storeDefinitions(repository, rid, definitions);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
List<File> files = Files.list(path).filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
for (File file : files) {
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), file.getName());
repository.putContentToFile(fileRef, stream, mediaType);
}
} catch (IllegalArgumentException | IOException e) {
throw new IllegalStateException(e);
}
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class WriterUtils method storeDefinitions.
// FIXME this used to be targeting Definitions, check whether that was necessary
public static void storeDefinitions(IRepository repository, TDefinitions definitions, boolean overwrite, Path dir) {
Path path = null;
try {
path = Files.createTempDirectory("winery");
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.debug("Store definition: {}", definitions.getId());
saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
TDefinitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
CsarImporter csarImporter = new CsarImporter(repository);
List<Exception> exceptions = new ArrayList<>();
cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
csarImporter.setNamespace(entry, namespace);
String id = ModelUtilities.getId(entry);
Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
if (repository.exists(wid)) {
if (overwrite) {
try {
repository.forceDelete(wid);
} catch (IOException e) {
exceptions.add(e);
}
} else {
return;
}
}
if (entry instanceof TArtifactTemplate) {
List<TArtifactReference> artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
artifactReferences.stream().filter(Objects::nonNull).forEach(ref -> {
String reference = ref.getReference();
URI refURI;
try {
refURI = new URI(reference);
} catch (URISyntaxException e) {
LOGGER.error("Invalid URI {}", reference);
return;
}
if (refURI.isAbsolute()) {
return;
}
Path artifactPath = dir.resolve(reference);
if (!Files.exists(artifactPath)) {
LOGGER.error("File not found {}", artifactPath);
return;
}
ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
try (InputStream is = Files.newInputStream(artifactPath);
BufferedInputStream bis = new BufferedInputStream(is)) {
MediaType mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
repository.putContentToFile(aFile, bis, mediaType);
} catch (IOException e) {
LOGGER.error("Could not read artifact template file: {}", artifactPath);
}
});
}
final TDefinitions part = BackendUtils.createWrapperDefinitions(wid, repository);
part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
String content = BackendUtils.getXMLAsString(part, true, repository);
try {
repository.putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
} catch (Exception e) {
exceptions.add(e);
}
});
}
Aggregations