@SuppressWarnings("deprecation")
  private WaveDevSettings getWaveDevSettings(
      final ResourceSet set, final SoftPkg softPkg, final String codegenId, final String templateId)
      throws CoreException {
    WaveDevSettings retVal = null;
    // First, try to get the .wavedev from disk. This will throw an exception if it fails.
    try {
      retVal =
          CodegenUtil.getWaveDevSettings(
              set.getResource(CodegenUtil.getSettingsURI(softPkg), true));
    } catch (final Exception e) {
      System.out.println("Unable to find the settings file, inferring defaults");
    }

    // if we weren't able to find the wavedev, create it
    if (retVal == null) {
      retVal = CodegenFactory.eINSTANCE.createWaveDevSettings();
      // Recreate the basic settings for each implementation
      // This makes assumptions that the defaults are selected for everything
      for (final Implementation impl : softPkg.getImplementation()) {
        final ImplementationSettings settings =
            CodegenFactory.eINSTANCE.createImplementationSettings();
        final String lang = impl.getProgrammingLanguage().getName();

        // Find the code generator if specified, otherwise pick the first one returned by the
        // registry
        ICodeGeneratorDescriptor codeGenDesc = null;
        if (codegenId != null) {
          codeGenDesc = RedhawkCodegenActivator.getCodeGeneratorsRegistry().findCodegen(codegenId);
        } else {
          final ICodeGeneratorDescriptor[] codeGens =
              RedhawkCodegenActivator.getCodeGeneratorsRegistry().findCodegenByLanguage(lang);
          if (codeGens.length > 0) {
            codeGenDesc = codeGens[0];
          }
        }

        // Proceed if we found one
        if (codeGenDesc != null) {
          final IScaComponentCodegen generator = codeGenDesc.getGenerator();

          // Assume that there is <name>[/].+<other> format for the entrypoint
          // Pick out <name> for both the output dir and settings name
          final String lf = impl.getCode().getEntryPoint();
          final String name = lf.substring(0, lf.indexOf('/'));

          // Set the generator, settings name and output directory
          settings.setGeneratorId(generator.getClass().getCanonicalName());
          settings.setName(name);
          settings.setOutputDir(lf.substring(0, lf.lastIndexOf('/')));

          // Find the template if specified, otherwise pick the first selectable and defaultable one
          // returned by the registry
          ITemplateDesc templateDesc = null;
          if (templateId != null) {
            templateDesc =
                RedhawkCodegenActivator.getCodeGeneratorTemplatesRegistry()
                    .findTemplate(templateId);
          } else {
            final ITemplateDesc[] templates =
                RedhawkCodegenActivator.getCodeGeneratorTemplatesRegistry()
                    .findTemplatesByCodegen(settings.getGeneratorId());
            for (final ITemplateDesc itd : templates) {
              if (itd.isSelectable() && !itd.notDefaultableGenerator()) {
                templateDesc = itd;
                break;
              }
            }
          }

          // If we found the template, use it
          if (templateDesc != null) {
            // Set the properties to their default values
            for (final IPropertyDescriptor prop : templateDesc.getPropertyDescriptors()) {
              final Property p = CodegenFactory.eINSTANCE.createProperty();
              p.setId(prop.getKey());
              p.setValue(prop.getDefaultValue());
              settings.getProperties().add(p);
            }
            // Set the template
            settings.setTemplate(templateDesc.getId());
          } else {
            System.err.println("Unable to find a valid template! Desired: " + templateId);
          }
        } else {
          System.err.println("Unable to find a valid Code Generator! Desired: " + codegenId);
        }
        // Save the created settings
        retVal.getImplSettings().put(impl.getId(), settings);
      }
      // Create the URI to the .wavedev file
      final URI uri =
          URI.createPlatformResourceURI(
              softPkg.getName() + "/." + softPkg.getName() + ".wavedev", false);
      final Resource res = set.createResource(uri);

      // Add the WaveDevSettings to the resource and save to disk to persist the newly created
      // WaveDevSettings
      res.getContents().add(retVal);
      try {
        res.save(null);
      } catch (final IOException e) {

      }
    }
    return retVal;
  }
  // TODO - turn this into an OSGi command
  private void generate_code(
      final String project_path,
      final String lang,
      String codegenId,
      final String templateId,
      final String[] preserveFiles,
      final NullProgressMonitor progressMonitor)
      throws CoreException {
    final SubMonitor monitor = SubMonitor.convert(progressMonitor, 2);

    final ResourceSet set = ScaResourceFactoryUtil.createResourceSet();

    final IPath projectPath = new Path(project_path);
    final IProject project = openProject(projectPath);
    final SoftPkg softPkg = getSoftPkg(project);

    if (softPkg == null) {
      throw new IllegalStateException("Could not load spd.xml for project");
    }

    // Create or open the existing settings
    final WaveDevSettings waveDev = getWaveDevSettings(set, softPkg, codegenId, templateId);
    if (waveDev == null) {
      throw new IllegalStateException("Could not load wavedev settings for project");
    }

    final EMap<String, ImplementationSettings> implSet = waveDev.getImplSettings();

    // Try generate each implementation, or just the specified language
    for (final Implementation impl : softPkg.getImplementation()) {
      final String currLang = impl.getProgrammingLanguage().getName();
      if ((lang != null) && !lang.equals(currLang.toLowerCase())) {
        continue;
      }

      // Prepare for generation
      final ImplementationSettings settings = implSet.get(impl.getId());
      final ArrayList<FileToCRCMap> crcMap = new ArrayList<FileToCRCMap>();

      System.out.println("\n\nGenerating " + currLang + " code for " + softPkg.getName());

      // Validate the settings name
      final String implName = CodegenFileHelper.safeGetImplementationName(impl, settings);
      if (!implName.equals(CodegenUtil.getValidName(implName))) {
        System.err.println("Invalid characters in implementation name for " + implName);
        continue;
      } else if (settings.getGeneratorId() != null) {
        // Find the desired code generator
        codegenId = settings.getGeneratorId();
        final ICodeGeneratorDescriptor codeGenDesc =
            RedhawkCodegenActivator.getCodeGeneratorsRegistry().findCodegen(codegenId);
        if (codeGenDesc == null) {
          System.err.println(
              "The code generator(" + codegenId + ") for this implementation could not be found.");
          continue;
        }
        // Get the actual code generator
        final IScaComponentCodegen generator = codeGenDesc.getGenerator();
        // Get files to generate
        final Set<FileStatus> fileStatusSet = generator.getGeneratedFilesStatus(settings, softPkg);
        final Set<String> fileList = new HashSet<String>();
        for (FileStatus s : fileStatusSet) {
          fileList.add(s.getFilename());
        }
        // Remove files we don't want to delete
        if (preserveFiles.length != 0) {
          if ("*".equals(preserveFiles[0])) {
            fileList.clear();
          } else {
            for (final String f : preserveFiles) {
              if (fileList.contains(f)) {
                fileList.remove(f);
              }
            }
          }
        }
        // Generate the files
        final IStatus status =
            generator.generate(
                settings,
                impl,
                System.out,
                System.err,
                monitor.newChild(1),
                fileList.toArray(new String[0]),
                generator.shouldGenerate(),
                crcMap);
        // Save the workspace
        final WorkspaceModifyOperation operation =
            new WorkspaceModifyOperation() {

              @Override
              protected void execute(final IProgressMonitor monitor)
                  throws CoreException, InvocationTargetException, InterruptedException {
                final IStatus saveStatus = ResourcesPlugin.getWorkspace().save(true, monitor);
                // Check the save results, hopefully this worked
                if (!saveStatus.isOK()) {
                  System.err.println(
                      "Generated files, but there was a problem saving the workspace: "
                          + saveStatus.getMessage());
                }
              }
            };
        try {
          operation.run(monitor.newChild(1));
        } catch (final InvocationTargetException e) {
          throw new CoreException(
              new Status(
                  IStatus.ERROR, CodegeneratorApplication.PLUGIN_ID, "Error saving resources", e));
        } catch (final InterruptedException e) {
          throw new CoreException(
              new Status(
                  IStatus.ERROR, CodegeneratorApplication.PLUGIN_ID, "Error saving resources", e));
        }

        // Check the results
        if (!status.isOK()) {
          System.err.println(
              "\nErrors occurred generating " + currLang + " code: " + status.getMessage());
          continue;
        } else {
          System.out.println("\nDone generating " + currLang + " code!");
        }
      } else {
        System.err.println(
            "No generator specified for implementation: " + implName + ". No code generated.");
      }
    }

    project.build(IncrementalProjectBuilder.FULL_BUILD, monitor.newChild(1));
  }