packagecmd.cpp

00001 
00002 // Name:        packagecmd.cpp
00003 // Purpose:     wxPackageCommand, wxPackageCommandArray, wxPackageCommandSet
00004 // Author:      Francesco Montorsi
00005 // Modified by:
00006 // Created:     2006-11-07
00007 // RCS-ID:      $Id: packagecmd.cpp,v 1.4 2007/01/28 15:56:11 frm Exp $
00008 // Copyright:   (c) Francesco Montorsi
00009 // Licence:     wxWidgets licence
00011 
00012 
00013 // For compilers that support precompilation, includes "wx/wx.h".
00014 #include "wx/wxprec.h"
00015 
00016 #ifdef __BORLANDC__
00017 #pragma hdrstop
00018 #endif
00019 
00020 #ifndef WX_PRECOMP
00021     #include "wx/wx.h"
00022 #endif
00023 
00024 // includes
00025 #include <wx/fileconf.h>
00026 #include <wx/filename.h>
00027 #include <wx/process.h>
00028 
00029 #include "wxp/packagecmd.h"
00030 
00031 
00032 
00033 // RTTI
00034 IMPLEMENT_DYNAMIC_CLASS(wxPackageCommand, wxObject)
00035 
00036 
00037 // wx array implementations
00038 #include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
00039 WX_DEFINE_OBJARRAY(wxPackageCommandArrayHelper);
00040 
00041 
00042 // global objects
00043 wxPackageCommand wxEmptyPackageCommand;
00044 wxPackageCommandArray wxEmptyPackageCommandArray;
00045 
00046 
00047 
00048 // ----------------------------------------------------------------------------
00049 // wxPackageCommand
00050 // ----------------------------------------------------------------------------
00051 
00052 wxString wxPackageCommand::GetProgram() const
00053 {
00054     // NB: do not access m_strCmd directly but rather use GetCommand()
00055     //     which performs substitutions inside it
00056     wxString cmd = GetCommand();
00057 
00058 #ifdef __WXMSW__
00059     size_t n = cmd.Find(wxT(".exe"));
00060     cmd = cmd.Left(n == wxNOT_FOUND ? 0 : n+4);
00061 #else
00062     cmd = cmd.BeforeFirst(wxT(' '));
00063 #endif
00064 
00065     return cmd;
00066 }
00067 
00068 wxString wxPackageCommand::GetProgramWithPath(const wxCompilerSettings &sett) const
00069 {
00070     return sett.GetToolPath(GetProgram());
00071 }
00072 
00073 wxString wxPackageCommand::GetProgramArguments() const
00074 {
00075     // NB: do not access m_strCmd directly but rather use GetCommand()
00076     //     which performs substitutions inside it
00077     wxString cmd = GetCommand();
00078 #ifdef __WXMSW__
00079     size_t n = cmd.Find(wxT(".exe"));
00080     return cmd.Mid(n == wxNOT_FOUND ? 0 : n+4);
00081 #else
00082     return cmd.AfterFirst(wxT(' '));
00083 #endif
00084 }
00085 
00086 void wxPackageCommand::UpdateWorkingDirectory(const wxString &decompressionpath) const
00087 {
00088     // set our current directory (previous commands could have changed it)
00089     // NB: the + wxT("/") is important to force wxFileName to consider the
00090     //     first argument a directory path
00091     wxFileName path(GetWorkingPath() + wxT("/"), wxPATH_UNIX);
00092 
00093     if (path.MakeAbsolute(decompressionpath) && path.DirExists())
00094         wxSetWorkingDirectory(path.GetFullPath());
00095     else
00096         wxSetWorkingDirectory(decompressionpath);
00097 }
00098 
00099 long wxPackageCommand::Run(wxProcess *proc) const
00100 {
00101     if (proc) proc->Redirect();
00102 
00103     // NB: do not access m_strCmd directly but rather use GetCommand()
00104     //     which performs substitutions inside it
00105     wxString cmd = GetCommand();
00106     wxLogDebug(wxT("Launching command '%s' in dir '%s'"), 
00107                cmd.c_str(), wxGetCwd().c_str());
00108 
00109     // launch it !
00110     return wxExecute(cmd, wxEXEC_ASYNC|wxEXEC_MAKE_GROUP_LEADER, proc);
00111 }
00112 
00113 
00114 // ----------------------------------------------------------------------------
00115 // wxPackageCommandArray
00116 // ----------------------------------------------------------------------------
00117 
00118 wxPackageCommand wxPackageCommandArray::GetSubstitutedCmd(size_t n) const
00119 {
00120     wxPackageCommand ret(Item(n));
00121 
00122     // GetCommand() will do substitutions using wxPackageCommand's own keyword hashmap
00123     wxString text(ret.GetCommand());
00124     text.Replace(wxT("$(target)"), GetSelectedTarget());
00125 
00126     ret.SetCommand(text);
00127     return ret;
00128 }
00129 
00130 wxPackageCommandArray wxPackageCommandArray::GetValidCommandsFor(const wxPackage &pkg) const
00131 {
00132     wxPackageCommandArray ret;
00133     for (size_t i=0; i<GetCount(); i++)
00134         if (Item(i).GetCondition().IsValidForPackage(pkg))
00135             ret.Add(Item(i));
00136 
00137     ret.CopyTargets(*this);
00138     return ret;
00139 }
00140 
00141 
00142 
00143 
00144 // ----------------------------------------------------------------------------
00145 // wxPackageCommandSet
00146 // ----------------------------------------------------------------------------
00147 
00148 bool wxPackageCommandSet::IsUsedBy(const wxPackageCommandArray *arrays,
00149                                    wxStringHashMap &touse) const
00150 {
00151     // VERY IMPORTANT:
00152     // We will say that the given commandset is "contained"
00153     // in this array if the two coincide. This is because in this way
00154     // we avoid problems regarding e.g. the command order.
00155     // in fact, if there are commands in this array which do not belong to
00156     // the given commandset, interleaved with those which belong, then
00157     // it's impossible to correctly separe user's commands from the commandset!
00158 //    if (GetCount() != arr.GetCount())
00159 //        return false;
00160 
00161     size_t checked[wxPBSS_MAX] = { 0, 0, 0, 0 };
00162     for (size_t j=0; j<GetCount(); j++)
00163     {
00164         for (size_t i=0; i<wxPBSS_MAX; i++)
00165         {
00166             if (Item(j).GetCondition().IsValidForStage(wxIdx2PackageBuildSystemStage(i)))
00167             {
00168                 // our j-th command applies to the i-th stage.
00169                 // checked[i] is the number of our commands valid for the i-th stage
00170                 // found up to now; we must check that the checked[i]-th command of
00171                 // array[i] is identic to our j-th item:
00172 
00173                 if (arrays[i].GetCount() <= checked[i])
00174                     return false;       // not enough commands in user's array
00175 
00176                 // order is important!
00177                 if (arrays[i].Item(checked[i]) != Item(j))
00178                     return false;
00179                 else
00180                     checked[i]++;
00181             }
00182         }
00183     }
00184 
00185     // when we loaded the commandset (see wxPackageXMLDescriptor::LoadStages)
00186     // we put the hashmap of commandset vars into all commands as keywords to
00187     // be substituted; so just pick one command:
00188     wxASSERT(GetCount() > 0);
00189     touse = arrays[0].Item(0).GetKeywords();
00190 
00191     return true;
00192 }

Generated on Thu Feb 1 22:14:31 2007 for wxWidgets Package Manager by  doxygen 1.5.1-p1