001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.xoo.lang;
021
022import com.google.common.base.Splitter;
023import org.apache.commons.io.FileUtils;
024import org.apache.commons.lang.StringUtils;
025import org.sonar.api.batch.fs.FilePredicates;
026import org.sonar.api.batch.fs.FileSystem;
027import org.sonar.api.batch.fs.InputFile;
028import org.sonar.api.batch.sensor.Sensor;
029import org.sonar.api.batch.sensor.SensorContext;
030import org.sonar.api.batch.sensor.SensorDescriptor;
031import org.sonar.api.utils.log.Logger;
032import org.sonar.api.utils.log.Loggers;
033import org.sonar.xoo.Xoo;
034
035import java.io.File;
036import java.io.IOException;
037import java.util.Iterator;
038import java.util.List;
039
040/**
041 * Parse files *.xoo.deps
042 */
043public class DependencySensor implements Sensor {
044
045  private static final Logger LOG = Loggers.get(DependencySensor.class);
046
047  private static final String DEPS_EXTENSION = ".deps";
048
049  private void processDependencies(InputFile inputFile, SensorContext context) {
050    File ioFile = inputFile.file();
051    File depsFile = new File(ioFile.getParentFile(), ioFile.getName() + DEPS_EXTENSION);
052    if (depsFile.exists()) {
053      LOG.debug("Processing " + depsFile.getAbsolutePath());
054      try {
055        List<String> lines = FileUtils.readLines(depsFile, context.fileSystem().encoding().name());
056        int lineNumber = 0;
057        for (String line : lines) {
058          lineNumber++;
059          if (StringUtils.isBlank(line) || line.startsWith("#")) {
060            continue;
061          }
062          processLine(depsFile, lineNumber, context, line, inputFile);
063        }
064      } catch (IOException e) {
065        throw new IllegalStateException(e);
066      }
067    }
068  }
069
070  private void processLine(File coverPerTest, int lineNumber, SensorContext context, String line, InputFile file) {
071    try {
072      Iterator<String> split = Splitter.on(":").split(line).iterator();
073      String otherFileRelativePath = split.next();
074      FileSystem fs = context.fileSystem();
075      InputFile otherFile = fs.inputFile(fs.predicates().hasRelativePath(otherFileRelativePath));
076      if (otherFile == null) {
077        throw new IllegalStateException("Unable to find file " + otherFileRelativePath);
078      }
079      int weight = Integer.parseInt(split.next());
080      context.newDependency()
081        .from(file)
082        .to(otherFile)
083        .weight(weight)
084        .save();
085    } catch (Exception e) {
086      throw new IllegalStateException("Error processing line " + lineNumber + " of file " + coverPerTest.getAbsolutePath(), e);
087    }
088  }
089
090  @Override
091  public void describe(SensorDescriptor descriptor) {
092    descriptor
093      .name("Xoo Dependency Sensor")
094      .onlyOnLanguages(Xoo.KEY)
095      .onlyOnFileType(InputFile.Type.MAIN);
096  }
097
098  @Override
099  public void execute(SensorContext context) {
100    FileSystem fs = context.fileSystem();
101    FilePredicates p = fs.predicates();
102    for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(InputFile.Type.MAIN)))) {
103      processDependencies(file, context);
104    }
105  }
106}