eachImport()

in lib/generator.js [2344:2429]


  eachImport(imports, usedModels, innerModule, filepath, level) {
    this.imports = {};
    if (imports.length === 0) {
      return;
    }

    if (!this.config.pkgDir) {
      throw new Error(`Must specific pkgDir when have imports`);
    }

    let lock;
    const lockPath = path.join(this.config.pkgDir, '.libraries.json');
    if (fs.existsSync(lockPath)) {
      lock = JSON.parse(fs.readFileSync(lockPath, 'utf8'));
    }

    for (let i = 0; i < imports.length; i++) {
      const item = imports[i];
      const aliasId = item.lexeme;
      const main = item.mainModule;
      const inner = item.module;

      let moduleDir;
      if (this.config.libraries) {
        moduleDir = main ? this.config.libraries[main] : this.config.libraries[aliasId];
      }
      const innerPath = item.innerPath;
      const importNameSpace = _nameSpace(path.join(path.dirname(filepath), _upperPath(innerPath)));
      if (!moduleDir && innerPath) {
        let csPath = innerPath.replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, '');
        const className = this.getInnerClient(aliasId, csPath);  // Common,Util,User 
        // 这里设置 path.join(path.dirname(csPath),`${className}.cs`))而不是cspath.cs,因为文件名需要和类名保持一致
        innerModule.set(aliasId, path.join(path.dirname(csPath), `${className}.cs`));
        const aliasName = this.getAliasName(`${this.namespace}.${importNameSpace}`, className, aliasId);
        this.moduleClass.set(aliasId, {
          namespace: `${this.namespace}.${importNameSpace}`.replace(/\.$/, ''),
          className: className,
          aliasName: aliasName
        });
        this.imports[aliasId] = {
          namespace: `${this.namespace}.${importNameSpace}`.replace(/\.$/, ''),
          release: '',
          className: aliasId
        };
        continue;
      }
      let targetPath = '';
      if (moduleDir.startsWith('./') || moduleDir.startsWith('../')) {
        targetPath = path.join(this.config.pkgDir, moduleDir);
      } else if (moduleDir.startsWith('/')) {
        targetPath = moduleDir;
      } else {
        targetPath = path.join(this.config.pkgDir, lock[moduleDir]);
      }
      const pkgPath = fs.existsSync(path.join(targetPath, 'Teafile')) ? path.join(targetPath, 'Teafile') : path.join(targetPath, 'Darafile');
      const pkg = JSON.parse(fs.readFileSync(pkgPath));
      let csharpConfig = pkg.csharp;
      pkg.releases = pkg.releases || {};
      if (!csharpConfig) {
        throw new Error(`The '${aliasId}' has no csharp supported.`);
      }
      csharpConfig.release = pkg.releases.csharp;
      this.imports[aliasId] = csharpConfig;
      let className = csharpConfig.className || 'Client';
      let namespace = csharpConfig.namespace;
      if (inner && pkg.exports[inner]) {
        let csPath = path.dirname(pkg.exports[inner]);
        const arr = csPath.split(path.sep).slice(1);
        const [filename] = pkg.exports[inner].split(path.sep).slice(-1);
        arr.map(key => {
          namespace += '.' + _upperFirst(key);
        });
        const processedFilename = filename === '.' ? '' : _upperFirst(filename.toLowerCase().replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, ''));
        const exportsName = csharpConfig.exports ? csharpConfig.exports[inner] : '';
        className = exportsName ? exportsName : `${processedFilename}Client`;
      }
      const aliasName = this.getAliasName(namespace, className, aliasId);
      this.moduleClass.set(aliasId, {
        namespace: namespace,
        className: className,
        aliasName: aliasName
      });

      this.moduleTypedef[aliasId] = csharpConfig.typedef;
    }
  }