Package org.jboss.forge.resources

Examples of org.jboss.forge.resources.DirectoryResource


      this.projectFactory = projectFactory;
   }

   public void doInit(@Observes final InitProject event)
   {
      DirectoryResource currentDirectory = shell.getCurrentDirectory();

      Project newProject = null;

      try
      {
         final DirectoryResource newRoot = projectFactory.findProjectRootRecusively(currentDirectory);
         if (newRoot != null)
         {
            Project oldProject = cp.getCurrent();

            Project temp = new BaseProject()
            {
               @Override
               public DirectoryResource getProjectRoot()
               {
                  return newRoot;
               }

               @Override
               public boolean exists()
               {
                  return false;
               }
            };

            cp.setCurrentProject(temp);

            if (oldProject != null)
            {
               DirectoryResource oldProjectRoot = oldProject.getProjectRoot();
               if (!newRoot.equals(oldProjectRoot))
               {
                  newProject = projectFactory.findProjectRecursively(currentDirectory);
               }
               else
View Full Code Here


   public void installFromLocalProject(
            @Option(description = "project directory", required = true) final Resource<?> projectFolder,
            @Option(name = "coordinates", type = PromptType.DEPENDENCY_ID, description = "the coordinates for the plugin (if in a multi-module repository)") final Dependency coordinates,
            final PipeOut out) throws Exception
   {
      DirectoryResource workspace = projectFolder.reify(DirectoryResource.class);
      if ((workspace == null) || !workspace.exists())
      {
         throw new IllegalArgumentException("Project folder must be specified.");
      }
      pluginManager.installFromProject(workspace, coordinates);
View Full Code Here

            @Option(name = "checkoutDir", description = "directory in which to clone the repository") final Resource<?> checkoutResource,
            @Option(name = "keepSources", description = "keep the sources after checking out", defaultValue = "false", flagOnly = true) final boolean keepSources,
            @Option(name = "coordinates", type = PromptType.DEPENDENCY_ID, description = "the coordinates for the plugin (if in a multi-module repository)") final Dependency coordinates,
            final PipeOut out) throws Exception
   {
      DirectoryResource buildDir;
      if (checkoutResource != null)
      {
         if (!(checkoutResource instanceof FileResource<?>))
         {
            throw new IllegalArgumentException("Checkout dir must be a directory path");
         }
         FileResource<?> checkoutDir = (FileResource<?>) checkoutResource;
         // Resource already exists
         if (checkoutDir.exists())
         {
            // Check if it is already a directory
            if (!checkoutDir.isDirectory())
            {
               throw new RuntimeException("Resource " + checkoutDir.getFullyQualifiedName()
                        + " is not a valid directory.");
            }
            buildDir = checkoutDir.reify(DirectoryResource.class);
            if (!shell.promptBoolean("Directory " + buildDir.getFullyQualifiedName()
                     + " already exists. Do you want to overwrite?", false))
            {
               throw new AbortedException("Directory " + buildDir.getFullyQualifiedName()
                        + " already exists");
            }
            buildDir.delete(true);
            buildDir.mkdirs();
         }
         else
         {
            // Resource does not exist. Create it
            checkoutDir.mkdirs();
            buildDir = checkoutDir.reify(DirectoryResource.class);
         }
      }
      else
      {
         buildDir = shell.getCurrentDirectory().createTempResource();
      }

      Git repo = null;
      try
      {
         ShellMessages.info(out, "Checking out plugin source files to [" + buildDir.getFullyQualifiedName()
                  + "] via 'git'");
         repo = GitUtils.clone(buildDir, gitRepo);

         Ref ref = null;
         String targetRef = refName;
         if (targetRef == null)
         {
            // Default to Forge runtime version if no Ref name is supplied.
            targetRef = environment.getRuntimeVersion();
         }

         if (targetRef != null)
         {
            // Try to find a Tag matching the given Ref name or runtime version
            Map<String, Ref> tags = repo.getRepository().getTags();
            ref = tags.get(targetRef);

            // Now try to find a matching Branch
            if (ref == null)
            {
               List<Ref> refs = GitUtils.getRemoteBranches(repo);
               for (Ref branchRef : refs)
               {
                  String branchName = branchRef.getName();
                  if (branchName != null && branchName.endsWith(targetRef))
                  {
                     ref = repo.branchCreate().setName(targetRef).setUpstreamMode(SetupUpstreamMode.TRACK)
                              .setStartPoint("origin/" + targetRef).call();
                  }
               }
            }

            // Now try to find a tag or branch with same Major.Minor.(x) version.
            if (ref == null)
            {
               // All
               List<String> sortedVersions = new ArrayList<String>();

               // Branches
               for (Ref branchRef : GitUtils.getRemoteBranches(repo))
               {
                  String branchName = branchRef.getName();
                  branchName = branchName.replaceFirst("refs/heads/", "");
                  if (InstalledPluginRegistry.isApiCompatible(targetRef, branchName))
                     sortedVersions.add(branchName);
               }

               // Tags

               // Branches
               for (String tag : tags.keySet())
               {
                  if (InstalledPluginRegistry.isApiCompatible(targetRef, tag))
                     sortedVersions.add(tag);
               }

               // Sort
               Collections.sort(sortedVersions);

               if (!sortedVersions.isEmpty())
               {
                  String version = sortedVersions.get(sortedVersions.size() - 1);
                  if (InstalledPluginRegistry.isApiCompatible(targetRef, version))
                  {
                     ref = tags.get(version);

                     if (ref == null)
                     {
                        ref = repo.branchCreate().setName(version).setUpstreamMode(SetupUpstreamMode.TRACK)
                                 .setStartPoint("origin/" + version).call();
                     }
                  }
               }
            }
         }

         if (ref == null)
         {
            ref = repo.getRepository().getRef("master");
         }

         if (ref != null)
         {
            ShellMessages.info(out, "Switching to branch/tag [" + ref.getName() + "]");
            GitUtils.checkout(repo, ref, false, SetupUpstreamMode.TRACK, false);
         }
         else if (refName != null)
         {
            throw new RuntimeException("Could not locate ref [" + targetRef + "] in repository ["
                     + repo.getRepository().getDirectory().getAbsolutePath() + "]");
         }
         else
         {
            ShellMessages.warn(
                     out,
                     "Could not find a Ref matching the current Forge version ["
                              + environment.getRuntimeVersion()
                              + "], building Plugin from HEAD.");
         }
         pluginManager.installFromProject(buildDir, coordinates);
      }
      finally
      {
         GitUtils.close(repo);
         if (buildDir != null)
         {
            if (keepSources)
            {
               ShellMessages.info(out,
                        "Sources are kept in [" + buildDir.getFullyQualifiedName() + "]");
            }
            else
            {
               ShellMessages.info(out,
                        "Cleaning up temp workspace [" + buildDir.getFullyQualifiedName()
                                 + "]");
               buildDir.delete(true);
            }
         }
      }

      ShellMessages.success(out, "Installed from [" + gitRepo + "] successfully.");
View Full Code Here

    * Aborts a forge update
    */
   @Command(value = "update-abort", help = "Aborts a previous forge update")
   public void updateAbort() throws IOException
   {
      DirectoryResource forgeHome = environment.getForgeHome();
      DirectoryResource updateDirectory = forgeHome.getChildDirectory(".update");
      if (updateDirectory.exists())
      {
         if (updateDirectory.delete(true))
         {
            ShellMessages.success(shell,
                     "Update files were deleted. Run 'forge update' if you want to update this installation again.");
         }
         else
View Full Code Here

         shell.println("1. Download the latest Forge version from http://forge.jboss.org and unzip in any place you may find convenient;");
         shell.println("2. In Window->Preferences, look for Forge->Installed Forge Runtimes and add the path to your installation and make it the default runtime choice;");
         shell.println("3. Start the Forge Console. You should see it is running the latest version.");
         return;
      }
      DirectoryResource forgeHome = environment.getForgeHome();
      DirectoryResource updateDir = forgeHome.getChildDirectory(".update");
      if (updateDir.exists())
      {
         ShellMessages
                  .warn(shell,
                           "There is an update pending. Restart Forge for the update to take effect. To abort this update, type 'forge update-abort'");
         return;
View Full Code Here

   {
      wait.start("Update in progress. Please wait");
      List<DependencyResource> resolvedArtifacts = resolver.resolveArtifacts(dependency);
      Assert.isTrue(resolvedArtifacts.size() == 1, "Artifact was not found");
      DependencyResource resource = resolvedArtifacts.get(0);
      DirectoryResource forgeHome = environment.getForgeHome();
      Files.unzip(resource.getUnderlyingResourceObject(), forgeHome.getUnderlyingResourceObject());

      DirectoryResource childDirectory = forgeHome.getChildDirectory(dependency.getArtifactId() + "-"
               + dependency.getVersion());

      DirectoryResource updateDirectory = forgeHome.getChildDirectory(".update");
      if (updateDirectory.exists())
      {
         updateDirectory.delete(true);
      }
      childDirectory.renameTo(updateDirectory);
      wait.stop();
      ShellMessages.success(shell, "Forge will now restart to complete the update...");
      System.exit(0);
View Full Code Here

            @Option(name = "finalName",
                     description = "The final artifact name of the new project") final String finalName,
            final PipeOut out
            ) throws IOException
   {
      DirectoryResource dir = null;
      String javaPackage = suggestedJavaPackage;
      if (!getValidPackagingTypes().contains(type))
      {
         throw new RuntimeException("Unsupported packaging type: " + type);
      }

      // FORGE-571
      if (javaPackage == null)
      {
         javaPackage = "com.example." + Packages.toValidPackageName(name);
      }
      boolean skipFolderPrompt = false;
      try
      {
         if (projectFolder instanceof FileResource<?>)
         {
            if (!projectFolder.exists())
            {
               ((FileResource<?>) projectFolder).mkdirs();
               dir = projectFolder.reify(DirectoryResource.class);
               skipFolderPrompt = true;
            }
            else if (projectFolder instanceof DirectoryResource)
            {
               dir = (DirectoryResource) projectFolder;
               skipFolderPrompt = true;
            }
            else
            {
               ShellMessages.error(out, "File exists but is not a directory [" + projectFolder.getFullyQualifiedName()
                        + "]");
            }
         }

         if (dir == null)
         {
            dir = shell.getCurrentDirectory().getChildDirectory(name);
         }
      }
      catch (ResourceException e)
      {
      }

      if (!skipFolderPrompt && (projectFactory.containsProject(dir)
               || !shell.promptBoolean("Use [" + dir.getFullyQualifiedName() + "] as project directory?")))
      {
         if (projectFactory.containsProject(dir))
         {
            ShellMessages.error(out, "[" + dir.getFullyQualifiedName()
                     + "] already contains a project; please use a different folder.");
         }

         if (shell.getCurrentResource() == null)
         {
            dir = ResourceUtil.getContextDirectory(factory.getResourceFrom(Files.getWorkingDirectory()));
         }
         else
         {
            dir = shell.getCurrentDirectory();
         }

         FileResource<?> newDir;
         do
         {
            newDir = shell.getCurrentDirectory();
            shell.println();
            if (!projectFactory.containsProject(newDir.reify(DirectoryResource.class)))
            {
               newDir = shell.promptFile(
                        "Where would you like to create the project? [Press ENTER to use the current directory: "
                                 + newDir + "]", dir);
            }
            else
            {
               newDir = shell.promptFile("Where would you like to create the project?");
            }

            if (!newDir.exists())
            {
               newDir.mkdirs();
               newDir = newDir.reify(DirectoryResource.class);
            }
            else if (newDir.isDirectory() && !projectFactory.containsProject(newDir.reify(DirectoryResource.class)))
            {
               newDir = newDir.reify(DirectoryResource.class);
            }
            else
            {
               ShellMessages.error(out, "That folder already contains a project [" + newDir.getFullyQualifiedName()
                        + "], please select a different location.");
               newDir = null;
            }

         }
         while ((newDir == null) || !(newDir instanceof DirectoryResource));

         dir = (DirectoryResource) newDir;
      }

      if (!dir.exists())
      {
         dir.mkdirs();
      }

      Project project = null;

      if (type.equals(PackagingType.JAR) || type.equals(PackagingType.BUNDLE))
      {
         project = projectFactory.createProject(dir,
                  DependencyFacet.class,
                  MetadataFacet.class,
                  JavaSourceFacet.class,
                  ResourceFacet.class);
      }
      else if (type.equals(PackagingType.WAR))
      {
         project = projectFactory.createProject(dir,
                  DependencyFacet.class,
                  MetadataFacet.class,
                  WebResourceFacet.class,
                  JavaSourceFacet.class,
                  ResourceFacet.class);
      }
      else
      {
         project = projectFactory.createProject(dir,
                  DependencyFacet.class,
                  MetadataFacet.class);
      }

      DirectoryResource parentDir = project.getProjectRoot().getParent().reify(DirectoryResource.class);
      if (parentDir != null)
      {
         for (ProjectAssociationProvider provider : providers)
         {
            if (provider.canAssociate(project, parentDir)
                     && shell.promptBoolean("Add new project as a sub-project of [" + parentDir.getFullyQualifiedName()
                              + "]?"))
            {
               provider.associate(project, parentDir);
            }
         }
      }

      MetadataFacet meta = project.getFacet(MetadataFacet.class);
      meta.setProjectName(name);
      meta.setTopLevelPackage(javaPackage);

      PackagingFacet packaging = project.getFacet(PackagingFacet.class);
      packaging.setPackagingType(type);

      DependencyFacet deps = project.getFacet(DependencyFacet.class);
      deps.addRepository(KnownRepository.JBOSS_NEXUS);

      if (project.hasFacet(JavaSourceFacet.class))
      {
         JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
         DirectoryResource sourceFolder = facet.getSourceFolder();
         createTopLevelPackage(sourceFolder, javaPackage);
         if (createMain)
         {
            facet.saveJavaSource(JavaParser
                     .create(JavaClass.class)
View Full Code Here

      return validTypes;
   }

   private DirectoryResource createTopLevelPackage(DirectoryResource sourceFolder, String javaPackage)
   {
      DirectoryResource directory = sourceFolder.getChildDirectory(Packages.toFileSyntax(javaPackage));
      directory.mkdirs();
      return directory;
   }
View Full Code Here

    * @param coordinates The maven coordinates information of the required plugin
    * @throws Abort
    */
   public void installFromProject(final DirectoryResource buildDir, final Dependency coordinates) throws Abort
   {
      DirectoryResource root = projectFactory.findProjectRootRecusively(buildDir);
      Project rootProject = projectFactory.findProject(root);
      if (rootProject == null)
      {
         throw new IllegalArgumentException("Unable to recognise plugin project in ["
                  + buildDir.getFullyQualifiedName() + "]");
View Full Code Here

      // 1) Check if the root project is a multimodule project
      if (projectDependency.getPackagingTypeEnum() == PackagingType.BASIC)
      {
         Project tempPluginProject = null;
         DirectoryResource projectDir = rootProject.getProjectRoot();
         for (String module : rootPom.getModules())
         {
            DirectoryResource moduleDir = projectDir.getChildDirectory(module);
            // 1.1) If yes, traverse the projects
            Project moduleProject = projectFactory.findProject(moduleDir);
            // If module is empty, avoid infinite recursion
            if (moduleProject.equals(rootProject))
            {
View Full Code Here

TOP

Related Classes of org.jboss.forge.resources.DirectoryResource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.