<commit_before>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library command_lish;

import 'dart:io';
import 'dart:json';
import 'dart:uri';

import '../../pkg/args/lib/args.dart';
import '../../pkg/http/lib/http.dart' as http;
import 'directory_tree.dart';
import 'git.dart' as git;
import 'io.dart';
import 'log.dart' as log;
import 'oauth2.dart' as oauth2;
import 'path.dart' as path;
import 'pub.dart';
import 'validator.dart';

/// Handles the `lish` and `publish` pub commands.
class LishCommand extends PubCommand {
  final description = "Publish the current package to pub.dartlang.org.";
  final usage = "pub publish [options]";
  final aliases = const ["lish", "lush"];

  ArgParser get commandParser {
    var parser = new ArgParser();
    parser.addOption('server', defaultsTo: 'https://pub.dartlang.org',
        help: 'The package server to which to upload this package');
    return parser;
  }

  /// The URL of the server to which to upload the package.
  Uri get server => new Uri.fromString(commandOptions['server']);

  Future _publish(packageBytes) {
    var cloudStorageUrl;
    return oauth2.withClient(cache, (client) {
      // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We
      // should report that error and exit.
      var newUri = server.resolve("/packages/versions/new.json");
      return client.get(newUri).chain((response) {
        var parameters = _parseJson(response);

        var url = _expectField(parameters, 'url', response);
        if (url is! String) _invalidServerResponse(response);
        cloudStorageUrl = new Uri.fromString(url);
        var request = new http.MultipartRequest('POST', cloudStorageUrl);

        var fields = _expectField(parameters, 'fields', response);
        if (fields is! Map) _invalidServerResponse(response);
        fields.forEach((key, value) {
          if (value is! String) _invalidServerResponse(response);
          request.fields[key] = value;
        });

        request.followRedirects = false;
        request.files.add(new http.MultipartFile.fromBytes(
            'file', packageBytes, filename: 'package.tar.gz'));
        return client.send(request);
      }).chain(http.Response.fromStream).transform((response) {
        var location = response.headers['location'];
        if (location == null) throw new PubHttpException(response);
        return location;
      }).chain((location) => client.get(location)).transform((response) {
        var parsed = _parseJson(response);
        if (parsed['success'] is! Map ||
            !parsed['success'].containsKey('message') ||
            parsed['success']['message'] is! String) {
          _invalidServerResponse(response);
        }
        log.message(parsed['success']['message']);
      });
    }).transformException((e) {
      if (e is PubHttpException) {
        var url = e.response.request.url;
        if (url.toString() == cloudStorageUrl.toString()) {
          // TODO(nweiz): the response may have XML-formatted information about
          // the error. Try to parse that out once we have an easily-accessible
          // XML parser.
          throw 'Failed to upload the package.';
        } else if (url.origin == server.origin) {
          var errorMap = _parseJson(e.response);
          if (errorMap['error'] is! Map ||
              !errorMap['error'].containsKey('message') ||
              errorMap['error']['message'] is! String) {
            _invalidServerResponse(e.response);
          }
          throw errorMap['error']['message'];
        }
      } else if (e is oauth2.ExpirationException) {
        log.error("Pub's authorization to upload packages has expired and "
        "can't be automatically refreshed.");
        return _publish(packageBytes);
      } else if (e is oauth2.AuthorizationException) {
        var message = "OAuth2 authorization failed";
        if (e.description != null) message = "$message (${e.description})";
        log.error("$message.");
        return oauth2.clearCredentials(cache).chain((_) =>
            _publish(packageBytes));
      } else {
        throw e;
      }
    });
  }

  Future onRun() {
    var files;
    return _filesToPublish.transform((f) {
      files = f;
      log.fine('Archiving and publishing ${entrypoint.root}.');
      return createTarGz(files, baseDir: entrypoint.root.dir);
    }).chain(consumeInputStream).chain((packageBytes) {
      // Show the package contents so the user can verify they look OK.
      var package = entrypoint.root;
      log.message(
          'Publishing "${package.name}" ${package.version}:\n'
          '${generateTree(files)}');

      // Validate the package.
      return _validate().chain((_) => _publish(packageBytes));
    });
  }

  /// The basenames of files that are automatically excluded from archives.
  final _BLACKLISTED_FILES = const ['pubspec.lock'];

  /// The basenames of directories that are automatically excluded from
  /// archives.
  final _BLACKLISTED_DIRECTORIES = const ['packages'];

  /// Returns a list of files that should be included in the published package.
  /// If this is a Git repository, this will respect .gitignore; otherwise, it
  /// will return all non-hidden files.
  Future<List<String>> get _filesToPublish {
    var rootDir = entrypoint.root.dir;

    // TODO(rnystrom): listDir() returns real file paths after symlinks are
    // resolved. This means if libDir contains a symlink, the resulting paths
    // won't appear to be within it, which confuses relativeTo(). Work around
    // that here by making sure we have the real path to libDir. Remove this
    // when #7346 is fixed.
    rootDir = new File(rootDir).fullPathSync();

    return Futures.wait([
      dirExists(join(rootDir, '.git')),
      git.isInstalled
    ]).chain((results) {
      if (results[0] && results[1]) {
        // List all files that aren't gitignored, including those not checked in
        // to Git.
        return git.run(["ls-files", "--cached", "--others"]);
      }

      return listDir(rootDir, recursive: true).chain((entries) {
        return Futures.wait(entries.map((entry) {
          return fileExists(entry).transform((isFile) {<commit_msg>Exclude .gitignored files from published package.<commit_after>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library command_lish;

import 'dart:io';
import 'dart:json';
import 'dart:uri';

import '../../pkg/args/lib/args.dart';
import '../../pkg/http/lib/http.dart' as http;
import 'directory_tree.dart';
import 'git.dart' as git;
import 'io.dart';
import 'log.dart' as log;
import 'oauth2.dart' as oauth2;
import 'path.dart' as path;
import 'pub.dart';
import 'validator.dart';

/// Handles the `lish` and `publish` pub commands.
class LishCommand extends PubCommand {
  final description = "Publish the current package to pub.dartlang.org.";
  final usage = "pub publish [options]";
  final aliases = const ["lish", "lush"];

  ArgParser get commandParser {
    var parser = new ArgParser();
    parser.addOption('server', defaultsTo: 'https://pub.dartlang.org',
        help: 'The package server to which to upload this package');
    return parser;
  }

  /// The URL of the server to which to upload the package.
  Uri get server => new Uri.fromString(commandOptions['server']);

  Future _publish(packageBytes) {
    var cloudStorageUrl;
    return oauth2.withClient(cache, (client) {
      // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We
      // should report that error and exit.
      var newUri = server.resolve("/packages/versions/new.json");
      return client.get(newUri).chain((response) {
        var parameters = _parseJson(response);

        var url = _expectField(parameters, 'url', response);
        if (url is! String) _invalidServerResponse(response);
        cloudStorageUrl = new Uri.fromString(url);
        var request = new http.MultipartRequest('POST', cloudStorageUrl);

        var fields = _expectField(parameters, 'fields', response);
        if (fields is! Map) _invalidServerResponse(response);
        fields.forEach((key, value) {
          if (value is! String) _invalidServerResponse(response);
          request.fields[key] = value;
        });

        request.followRedirects = false;
        request.files.add(new http.MultipartFile.fromBytes(
            'file', packageBytes, filename: 'package.tar.gz'));
        return client.send(request);
      }).chain(http.Response.fromStream).transform((response) {
        var location = response.headers['location'];
        if (location == null) throw new PubHttpException(response);
        return location;
      }).chain((location) => client.get(location)).transform((response) {
        var parsed = _parseJson(response);
        if (parsed['success'] is! Map ||
            !parsed['success'].containsKey('message') ||
            parsed['success']['message'] is! String) {
          _invalidServerResponse(response);
        }
        log.message(parsed['success']['message']);
      });
    }).transformException((e) {
      if (e is PubHttpException) {
        var url = e.response.request.url;
        if (url.toString() == cloudStorageUrl.toString()) {
          // TODO(nweiz): the response may have XML-formatted information about
          // the error. Try to parse that out once we have an easily-accessible
          // XML parser.
          throw 'Failed to upload the package.';
        } else if (url.origin == server.origin) {
          var errorMap = _parseJson(e.response);
          if (errorMap['error'] is! Map ||
              !errorMap['error'].containsKey('message') ||
              errorMap['error']['message'] is! String) {
            _invalidServerResponse(e.response);
          }
          throw errorMap['error']['message'];
        }
      } else if (e is oauth2.ExpirationException) {
        log.error("Pub's authorization to upload packages has expired and "
        "can't be automatically refreshed.");
        return _publish(packageBytes);
      } else if (e is oauth2.AuthorizationException) {
        var message = "OAuth2 authorization failed";
        if (e.description != null) message = "$message (${e.description})";
        log.error("$message.");
        return oauth2.clearCredentials(cache).chain((_) =>
            _publish(packageBytes));
      } else {
        throw e;
      }
    });
  }

  Future onRun() {
    var files;
    return _filesToPublish.transform((f) {
      files = f;
      log.fine('Archiving and publishing ${entrypoint.root}.');
      return createTarGz(files, baseDir: entrypoint.root.dir);
    }).chain(consumeInputStream).chain((packageBytes) {
      // Show the package contents so the user can verify they look OK.
      var package = entrypoint.root;
      log.message(
          'Publishing "${package.name}" ${package.version}:\n'
          '${generateTree(files)}');

      // Validate the package.
      return _validate().chain((_) => _publish(packageBytes));
    });
  }

  /// The basenames of files that are automatically excluded from archives.
  final _BLACKLISTED_FILES = const ['pubspec.lock'];

  /// The basenames of directories that are automatically excluded from
  /// archives.
  final _BLACKLISTED_DIRECTORIES = const ['packages'];

  /// Returns a list of files that should be included in the published package.
  /// If this is a Git repository, this will respect .gitignore; otherwise, it
  /// will return all non-hidden files.
  Future<List<String>> get _filesToPublish {
    var rootDir = entrypoint.root.dir;

    // TODO(rnystrom): listDir() returns real file paths after symlinks are
    // resolved. This means if libDir contains a symlink, the resulting paths
    // won't appear to be within it, which confuses relativeTo(). Work around
    // that here by making sure we have the real path to libDir. Remove this
    // when #7346 is fixed.
    rootDir = new File(rootDir).fullPathSync();

    return Futures.wait([
      dirExists(join(rootDir, '.git')),
      git.isInstalled
    ]).chain((results) {
      if (results[0] && results[1]) {
        // List all files that aren't gitignored, including those not checked
        // in to Git.
        return git.run(["ls-files", "--cached", "--others",
                        "--exclude-standard"]);
      }

      return listDir(rootDir, recursive: true).chain((entries) {
        return Futures.wait(entries.map((entry) {
          return fileExists(entry).transform((isFile) {
{'fraction_non_alphanumeric': 0.12744637545013307, 'fraction_numerical': 0.0026616564897447943, 'mean_word_length': 2.8248502994011977, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3922421', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3508, 'n_words': 1082}