Thursday 7 August 2014

Make a copy of a project

One facility I can't find in AndroidScript is the ability to create a working copy (under another name) of an androidscript app.

As I have a need for freezing versions of a tiny app (my file explorer) I wrote an AndroidScript app to do this for me.

It currently has the specific app detail hard-coded but it would be really simple to extend it to work with any app that is made up of only text files.

An spk of the app is at http://hudl.sgarman.net/public/spk/version.spk

The code looks like:
var srceName = "xplorer";
var destStem = "SJGexplorer_";
var minVersion = 3;
var appsPath = "/sdcard/AndroidScript";

var srcePath;
var destName;
var destPath;

//saves current version of app
//only currently works with app in one file

//Called when application is started.
function OnStart()
{
    if( destStem == "" ) destStem = srceName + "_";
    //Create a layout with objects vertically centered.
    lay = app.CreateLayout( "linear", "FillXY" );  
    //Create a text label and add it to layout. 
    txt = app.CreateText( "Freeze latest version" );
    txt.SetTextSize( 32 );
    lay.AddChild( txt );
    //Add layout to app.    
    app.AddLayout( lay );
    //make sure source app exists
    srcePath = appsPath + "/" + srceName;
    if( ! app.FolderExists(srcePath) ){
      alert( srcePath + " does not exist" );
      app.Exit();
    }  
    //get next available destination app name;   
    destName = nextAvailable( appsPath, destStem  );
    destPath = appsPath + "/" + destName;

    //check if user wants to continue
    var quest = "Copy " + srceName  + " to " + destName  + "?";
    var yesNo = app.CreateYesNoDialog( quest );
    yesNo.SetOnTouch( yesNo_OnTouch );

 }

function nextAvailable( path, stem ){
  //get next available app name
  var filename
  for (i = minVersion; i < 1000; i++) {
    filename = stem + ("00" + i ).slice (-3)
    if( ! app.FolderExists ( path + "/" + filename ) )
       return( filename );
   }
   //no number available
   return undefined
}

function yesNo_OnTouch( result ){
  if( result=="Yes" ) doCopy();
  else app.Exit();
}

No comments:

Post a Comment