示例#1
0
void EvnVarList::InsertVariable(const wxString& setName, const wxString& name, const wxString& value)
{
    wxString actualSetName;

    DoGetSetVariablesStr(setName, actualSetName);

    EnvMap set = GetVariables(actualSetName, false, wxEmptyString, wxEmptyString);
    if(!set.Contains(name)) {
        set.Put(name, value);
    }
    m_envVarSets[actualSetName] = set.String();
}
示例#2
0
EnvMap EvnVarList::GetVariables(
    const wxString& setName, bool includeWorkspaceEnvs, const wxString& projectName, const wxString& configName)
{
    EnvMap variables;
    wxString actualSetName;

    wxString currentValueStr = DoGetSetVariablesStr(setName, actualSetName);

    if(includeWorkspaceEnvs && !clCxxWorkspaceST::Get()->GetName().IsEmpty()) {
        currentValueStr.Trim().Trim(false);
        currentValueStr << wxT("\n");
        currentValueStr << clCxxWorkspaceST::Get()->GetEnvironmentVariabels();

        if(projectName.IsEmpty() == false) {
            currentValueStr.Trim().Trim(false);
            BuildConfigPtr buildConf = clCxxWorkspaceST::Get()->GetProjBuildConf(projectName, configName);
            if(buildConf) {
                currentValueStr << wxT("\n");
                currentValueStr << buildConf->GetEnvvars();
            }
        }
    }

    wxArrayString currentValues = wxStringTokenize(currentValueStr, wxT("\r\n"), wxTOKEN_STRTOK);
    for(size_t i = 0; i < currentValues.GetCount(); i++) {
        wxString entry = currentValues.Item(i);

        // remove any comment from the line
        int where = entry.Find(wxT("#"));
        if(where != wxNOT_FOUND) {
            entry = entry.Left(where);
        }

        entry.Trim().Trim(false);
        if(entry.IsEmpty()) {
            continue;
        }

        wxString varname = entry.BeforeFirst(wxT('='));
        wxString varvalue = entry.AfterFirst(wxT('='));

        // Expand macros (which are not environment variables)
        varvalue = MacroManager::Instance()->ExpandNoEnv(varvalue, projectName, configName);
        variables.Put(varname, varvalue);
    }
    return variables;
}
void EnvironmentConfig::ApplyEnv(wxStringMap_t *overrideMap, const wxString &project, const wxString &config)
{
    // We lock the CS here and it will be released in UnApplyEnv
    // this is safe to call without Locker since the UnApplyEnv 
    // will always be called after ApplyEnv (ApplyEnv and UnApplyEnv are 
    // protected functions that can only be called from EnvSetter class
    // which always call UnApplyEnv in its destructor)
    m_cs.Enter();
    ++m_envApplied;
    
    if ( m_envApplied > 1 ) {
        //CL_DEBUG("Thread-%d: Applying environment variables... (not needed)", (int)wxThread::GetCurrentId());
        return;
    }

    //CL_DEBUG("Thread-%d: Applying environment variables...", (int)wxThread::GetCurrentId());
    //read the environments variables
    EvnVarList vars;
    ReadObject(wxT("Variables"), &vars);

    // get the active environment variables set
    EnvMap variables = vars.GetVariables(wxEmptyString, true, project, config);

    // if we have an "override map" place all the entries from the override map
    // into the global map before applying the environment
    if(overrideMap) {
        wxStringMap_t::iterator it = overrideMap->begin();
        for(; it != overrideMap->end(); it++) {
            variables.Put(it->first, it->second);
        }
    }

    m_envSnapshot.clear();
    for (size_t i=0; i<variables.GetCount(); i++) {

        wxString key, val;
        variables.Get(i, key, val);

        //keep old value before changing it
        wxString oldVal(wxEmptyString);
        if( wxGetEnv(key, &oldVal) == false ) {
            oldVal = __NO_SUCH_ENV__;
        }

        // keep the old value, however, don't override it if it
        // already exists as it might cause the variable to grow in size...
        // Simple case:
        // PATH=$(PATH);\New\Path
        // PATH=$(PATH);\Another\New\Path
        // If we replace the value, PATH will contain the original PATH + \New\Path
        if ( m_envSnapshot.count( key ) == 0 ) {
            m_envSnapshot.insert( std::make_pair( key, oldVal ) );
        }

        // Incase this line contains other environment variables, expand them before setting this environment variable
        wxString newVal = DoExpandVariables(val);

        //set the new value
        wxSetEnv(key, newVal);
    }
}