Last active
          August 29, 2015 14:14 
        
      - 
      
- 
        Save alejandrobernardis/89842ec40e07e82cf203 to your computer and use it in GitHub Desktop. 
    ANT # Control de Versiones - Version Control
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package kc.ant; | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| import java.util.Properties; | |
| public class KCVersion { | |
| /** | |
| * | |
| * Arguments: | |
| * action => Accion a realizar | |
| * file => Path del archivo que almacena la configuracion | |
| * format => Formato de la version | |
| * | |
| * Formats: | |
| * 1 => Major | |
| * 2 => Major.Minor | |
| * 3 => Major.Minor.Build | |
| * 4 => Major.Minor.Build.Test | |
| * | |
| */ | |
| // Constants | |
| private final static String ACTION_INCREASE_MAJOR = "-mai"; | |
| private final static String ACTION_INCREASE_MINOR = "-mii"; | |
| private final static String ACTION_INCREASE_BUILD = "-bui"; | |
| private final static String ACTION_INCREASE_TEST = "-tei"; | |
| private final static String ACTION_INCREASE_ALL = "-increase"; | |
| private final static String ACTION_RESET_MAJOR = "-mar"; | |
| private final static String ACTION_RESET_MINOR = "-mir"; | |
| private final static String ACTION_RESET_BUILD = "-bur"; | |
| private final static String ACTION_RESET_TEST = "-ter"; | |
| private final static String ACTION_RESET_ALL = "-reset"; | |
| private final static String ACTION_GET_VERSION = "-version"; | |
| private final static String PROPERTY_HEADER = "Build Version for ANT. Do not edit!"; | |
| private final static String PROPERTY_MINOR = "project.version.minor"; | |
| private final static String PROPERTY_MAJOR = "project.version.major"; | |
| private final static String PROPERTY_BUILD = "project.version.build"; | |
| private final static String PROPERTY_TEST = "project.version.test"; | |
| private static final int NOT_FOUND = -1; | |
| // Variables | |
| private String _command; | |
| private String _error; | |
| private File _file; | |
| private String _fileName; | |
| private String _format; | |
| private Properties _properties; | |
| private String _versionMajor; | |
| private String _versionMinor; | |
| private String _versionBuild; | |
| private String _versionTest; | |
| // Constructor | |
| public KCVersion(String[] args) { | |
| _format = "4"; | |
| if (validate(args) != NOT_FOUND){ | |
| execute(null); | |
| } | |
| System.out.print(_error); | |
| } | |
| // Validate | |
| protected int validate(String[] args) { | |
| if (args != null && args.length > 0){ | |
| _command = args[0].toLowerCase(); | |
| if (args.length > 1 && args[1].length() > 0) { | |
| _fileName = args[1]; | |
| if (validateFile() != NOT_FOUND || creationFile() != NOT_FOUND) { | |
| String[] list = { | |
| ACTION_GET_VERSION, | |
| ACTION_INCREASE_ALL, | |
| ACTION_INCREASE_BUILD, | |
| ACTION_INCREASE_MAJOR, | |
| ACTION_INCREASE_MINOR, | |
| ACTION_INCREASE_TEST, | |
| ACTION_RESET_ALL, | |
| ACTION_RESET_BUILD, | |
| ACTION_RESET_MAJOR, | |
| ACTION_RESET_MINOR, | |
| ACTION_RESET_TEST | |
| }; | |
| for (String e : list) { | |
| if (e.equals(_command)) { | |
| if (args.length > 2 && args[2].length() > 0){ | |
| int value = Integer.parseInt(args[2]); | |
| _format = Integer.toString(Math.min(4, Math.max(1, value))); | |
| } _error = null; | |
| return 0; | |
| } | |
| } | |
| _error = "Incorrect usage, unrecognized command argument provided:" + _command; | |
| } | |
| } else { | |
| _error = "Incorrect usage, no file name provided."; | |
| } | |
| } else { | |
| _error = "Incorrect usage, no arguments provided."; | |
| } | |
| return NOT_FOUND; | |
| } | |
| // Execute | |
| protected String execute(String action){ | |
| _error = null; | |
| if (loadProperties()) { | |
| getVersion(); | |
| if (_command.equals(ACTION_GET_VERSION)) { | |
| _error = getVersion(); | |
| } else { | |
| // Reset | |
| boolean reset; | |
| if (_command.equals(ACTION_RESET_ALL) | |
| || _command.equals(ACTION_RESET_BUILD) | |
| || _command.equals(ACTION_RESET_MAJOR) | |
| || _command.equals(ACTION_RESET_MINOR) | |
| || _command.equals(ACTION_RESET_TEST)) { | |
| reset = true; | |
| } else { | |
| reset = false; | |
| } | |
| // Exec | |
| if (_command.equals(ACTION_INCREASE_ALL) | |
| || _command.equals(ACTION_RESET_ALL)) { | |
| _versionMajor = updateProperty(PROPERTY_MAJOR, reset); | |
| _versionMinor = updateProperty(PROPERTY_MINOR, reset); | |
| _versionBuild = updateProperty(PROPERTY_BUILD, true); | |
| _versionTest = updateProperty(PROPERTY_TEST, true); | |
| } else if (_command.equals(ACTION_INCREASE_MAJOR) | |
| || _command.equals(ACTION_RESET_MAJOR)) { | |
| _versionMajor = updateProperty(PROPERTY_MAJOR, reset); | |
| } else if (_command.equals(ACTION_INCREASE_MINOR) | |
| || _command.equals(ACTION_RESET_MINOR)) { | |
| _versionMinor = updateProperty(PROPERTY_MINOR, reset); | |
| } else if (_command.equals(ACTION_INCREASE_BUILD) | |
| || _command.equals(ACTION_RESET_BUILD)) { | |
| _versionBuild = updateProperty(PROPERTY_BUILD, reset); | |
| } else if (_command.equals(ACTION_INCREASE_TEST) | |
| || _command.equals(ACTION_RESET_TEST)) { | |
| _versionTest = updateProperty(PROPERTY_TEST, reset); | |
| } | |
| // Save | |
| try { | |
| FileOutputStream output = new FileOutputStream(_file); | |
| _properties.store(output, PROPERTY_HEADER); | |
| output.close(); | |
| if(action != null){ | |
| return ""; | |
| }else{ | |
| _error = getFormatVersion(_format); | |
| } | |
| } catch (final Exception e) { | |
| _error = "Could not be saved property list."; | |
| } | |
| } | |
| }else{ | |
| _error = "Could not be loaded property list."; | |
| } | |
| return _error; | |
| } | |
| // Properties | |
| protected String getVersion() { | |
| try { | |
| _versionMajor = ( getProperty(PROPERTY_MAJOR) != null ) | |
| ? getProperty(PROPERTY_MAJOR) : "0"; | |
| _versionMinor = ( getProperty(PROPERTY_MINOR) != null ) | |
| ? getProperty(PROPERTY_MINOR) : "0"; | |
| _versionBuild = ( getProperty(PROPERTY_BUILD) != null ) | |
| ? getProperty(PROPERTY_BUILD) : "0"; | |
| _versionTest = ( getProperty(PROPERTY_TEST) != null ) | |
| ? getProperty(PROPERTY_TEST) : "0"; | |
| } catch (Exception e) { | |
| _versionMajor = "0"; | |
| _versionMinor = "0"; | |
| _versionBuild = "0"; | |
| _versionTest = "0"; | |
| } | |
| return _versionMajor + "." + _versionMinor + "." | |
| + _versionBuild + "." + _versionTest; | |
| } | |
| protected String getFormatVersion(String format) { | |
| String result = new String(); | |
| int key = Integer.parseInt(format); | |
| if ( key > 0) { | |
| result = _versionMajor; | |
| if ( key > 1) { | |
| result += "." + _versionMinor; | |
| if ( key > 2) { | |
| result += "." + _versionBuild; | |
| if ( key > 3) { | |
| result += "." + _versionTest; | |
| } | |
| } | |
| } | |
| } | |
| return result; | |
| } | |
| protected boolean isProperty(String property) { | |
| if (!loadProperties()) { | |
| return false; | |
| } return _properties.containsKey(property); | |
| } | |
| protected String getProperty(String property) { | |
| if (!loadProperties()) { | |
| return null; | |
| } return _properties.getProperty(property).trim(); | |
| } | |
| protected boolean setProperty(String property, String value) { | |
| if (!loadProperties()) { | |
| return false; | |
| } return (_properties.setProperty(property, value) != null); | |
| } | |
| protected String updateProperty(String property, boolean reset) { | |
| int value = 0; | |
| if (!reset) { | |
| try { | |
| value = Integer.parseInt(getProperty(property)) + 1; | |
| } catch (final Exception e) {} | |
| } setProperty(property, Integer.toString(value)); | |
| return Integer.toString(value); | |
| } | |
| protected boolean loadProperties() { | |
| if (_properties == null) { | |
| try { | |
| FileInputStream file = new FileInputStream(_fileName); | |
| _properties = new Properties(); | |
| _properties.load(file); | |
| file.close(); | |
| } catch (final Exception e) { | |
| return false; | |
| } | |
| } return true; | |
| } | |
| // File | |
| protected int file() { | |
| _file = null; | |
| if (_fileName != null && _fileName.length() > 0) { | |
| _file = new File(_fileName); | |
| _error = null; | |
| return 0; | |
| } | |
| _error = "Could not define the file."; | |
| return NOT_FOUND; | |
| } | |
| protected int creationFile() { | |
| if (file() != NOT_FOUND) { | |
| try { | |
| _file.createNewFile(); | |
| if (execute(ACTION_RESET_ALL).equals("")) { | |
| _error = null; | |
| return 0; | |
| } | |
| } catch (Exception e) {} | |
| } | |
| _error = "The build files could not be created"; | |
| return NOT_FOUND; | |
| } | |
| protected int validateFile() { | |
| if (file() != NOT_FOUND) { | |
| if (_file.exists()) { | |
| if (_file.canRead()) { | |
| if (_file.canWrite()) { | |
| _error = null; | |
| return 0; | |
| } else { | |
| _error = "The build file can't write."; | |
| } | |
| } else { | |
| _error = "The build file can't read."; | |
| } | |
| } else { | |
| _error = "The build file no exists."; | |
| } | |
| } | |
| return NOT_FOUND; | |
| } | |
| // Main! | |
| public static void main(String[] args) { | |
| new KCVersion(args); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment